Commits

Anonymous committed 602f7e0

Now you can use a custom view on ModelChoiceField and AutoCompleteWidget.
It should be a subclass of autocomplete.views.AutoComplete.

  • Participants
  • Parent commits 48aa608

Comments (0)

Files changed (2)

File autocomplete/fields.py

 from autocomplete import autocomplete, AutoCompleteWidget
 
 class ModelChoiceField(forms.ModelChoiceField):
+    """
+    A ModelChoiceField which uses an autocomplete widget, instead of an html
+    select tag, to render its choices.
+    """
     widget = AutoCompleteWidget
 
-    def __init__(self, ac_name, reverse_label=True, view_name='autocomplete', **kwargs):
+    def __init__(self, ac_name, reverse_label=True, view=autocomplete,
+                 widget=None, **kwargs):
         self.ac_name = ac_name
-        self.widget = self.widget(ac_name, True, reverse_label, view_name)
-        forms.Field.__init__(self, **kwargs)
+        self.view = view
+        if widget is None:
+            widget = self.widget(ac_name, True, reverse_label, view)
+        forms.Field.__init__(self, widget=widget, **kwargs)
 
     def _get_queryset(self):
-        return autocomplete.settings[self.ac_name][0]
+        return self.view.settings[self.ac_name][0]
     queryset = property(_get_queryset, forms.ModelChoiceField._set_queryset)
 
     @property
     def to_field_name(self):
-        return autocomplete.settings[self.ac_name][3]
+        return self.view.settings[self.ac_name][3]

File autocomplete/widgets.py

               'js/yui_autocomplete.js')
 
     def __init__(self, ac_name, force_selection=True, reverse_label=True,
-                 view_name='autocomplete', attrs=None):
+                 view=autocomplete, attrs=None):
         super(AutoCompleteWidget, self).__init__(attrs)
         self.ac_name = ac_name
         self.force_selection = bool(force_selection)
         self.reverse_label = reverse_label
-        self.view_name = view_name
+        self.view = view
     
     def render(self, name, value, attrs=None, choices=()):
-        url = reverse(self.view_name, args=[self.ac_name])
+        url = reverse(self.view, args=[self.ac_name])
         force_selection = ('false', 'true')[self.force_selection]
         if not value:
             value = hidden_value = u''
         elif self.reverse_label:
             hidden_value = force_unicode(value)
-            value = autocomplete.reverse_label(self.ac_name, value)
+            value = self.view.reverse_label(self.ac_name, value)
         else:
             value = hidden_value = force_unicode(value)
         attrs = flatatt(self.build_attrs(attrs))