Commits

leo-the-manic committed ddf9b31 Draft

AutoCompleteSelectWidget checks for non-multiwidget postdata

Comments (0)

Files changed (2)

selectable/forms/widgets.py

         ]
         super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
 
+    def get_compatible_postdata(self, data, name):
+        """Get postdata built for a normal <select> element.
+
+        Django MultiWidgets create post variables like ``foo_0`` and ``foo_1``,
+        and this behavior is not cleanly overridable.  Non-multiwidgets, like
+        Select, get simple names like ``foo``. In order to keep this widget
+        compatible with requests designed for traditional select widgets,
+        search postdata for a name like ``foo`` and return that value.
+
+        This will return ``None`` if a ``<select>``-compatibile post variable
+        is not found.
+
+        """
+        return data.get(name, None)
+
     def value_from_datadict(self, data, files, name):
         value = super(AutoCompleteSelectWidget, self).value_from_datadict(data, files, name)
+        if not value[1]:
+            compatible_postdata = self.get_compatible_postdata(data, name)
+            if compatible_postdata:
+                value[1] = compatible_postdata
         if not self.allow_new:
             return value[1]
         return value

selectable/tests/widgets.py

         self.assertTrue('data-selectable-options' in attrs)
         self.assertEqual(attrs['data-selectable-options'], json.dumps(options))
 
+    def test_postdata_compatible_with_select(self):
+        "Checks postdata for values that a select widget would generate."
+        postdata = {'fruit': '1'}
+        widget = self.get_widget_instance()
+        widget_val = widget.value_from_datadict(postdata, [], 'fruit')
+        self.assertEquals(widget_val, '1')
+
 
 class AutoComboboxWidgetTestCase(BaseWidgetTestCase):
     widget_cls = widgets.AutoComboboxWidget