Commits

Mark Lavin committed acb0364

Adjust fields/widgets for compatibility with ModelChoiceField. See #40.

Comments (0)

Files changed (2)

selectable/forms/fields.py

     def to_python(self, value):
         if value in EMPTY_VALUES:
             return None
+        lookup =self.lookup_class()
         if isinstance(value, list):
             # Input comes from an AutoComplete widget. It's two
             # components: text and id
             if len(value) != 2:
                 raise ValidationError(self.error_messages['invalid_choice'])
-            lookup =self.lookup_class()
             if value[1] in EMPTY_VALUES:
                 if not self.allow_new:
                     if value[0]:
                 value = lookup.get_item(value[1])
                 if value is None:
                     raise ValidationError(self.error_messages['invalid_choice'])
+        else:
+            value = lookup.get_item(value)
+            if value is None:
+                raise ValidationError(self.error_messages['invalid_choice'])
         return value
 
 

selectable/forms/widgets.py

             return [item_value, value]
         return [None, None]
 
+    def value_from_datadict(self, data, files, name):
+        value = super(AutoCompleteSelectWidget, self).value_from_datadict(data, files, name)
+        if not self.allow_new:
+            return value[1]
+        return value
+
 
 class AutoComboboxWidget(AutoCompleteWidget, SelectableMediaMixin):
 
             return [item_value, value]
         return [None, None]
 
+    def value_from_datadict(self, data, files, name):
+        value = super(AutoComboboxSelectWidget, self).value_from_datadict(data, files, name)
+        if not self.allow_new:
+            return value[1]
+        return value
+
 
 class LookupMultipleHiddenInput(forms.MultipleHiddenInput):