Mark Lavin avatar 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):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.