def test_custom_widgets(self): formfield = utils.autocomplete_formfield('testapp.simple', widget_class=custom_widgets.CustomAutocompleteWidget, view=autocomplete) self.assertIsInstance(formfield.widget, custom_widgets.CustomAutocompleteWidget) formfield = utils.autocomplete_formfield(Dummy.friends, multiple_widget_class=custom_widgets.CustomMultipleAutocompleteWidget, view=autocomplete) self.assertIsInstance(formfield.widget, custom_widgets.CustomMultipleAutocompleteWidget)
def test_custom_widgets(self): formfield = utils.autocomplete_formfield( 'testapp.simple', widget_class=custom_widgets.CustomAutocompleteWidget, view=autocomplete) self.assertIsInstance(formfield.widget, custom_widgets.CustomAutocompleteWidget) formfield = utils.autocomplete_formfield( Dummy.friends, multiple_widget_class=custom_widgets. CustomMultipleAutocompleteWidget, view=autocomplete) self.assertIsInstance(formfield.widget, custom_widgets.CustomMultipleAutocompleteWidget)
def autocomplete_formfield(self, ac_id, formfield=None, **kwargs): return autocomplete_formfield( ac_id, formfield, self.autocomplete_view, AdminAutocompleteWidget, AdminMultipleAutocompleteWidget, **kwargs)
def test_default_values(self): formfield = utils.autocomplete_formfield(Dummy.user2, view=autocomplete) self.assertIsInstance(formfield, forms.ModelChoiceField) self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget) formfield = utils.autocomplete_formfield(Dummy.friends, view=autocomplete) self.assertIsInstance(formfield, forms.ModelMultipleChoiceField) self.assertIsInstance(formfield.widget, widgets.MultipleAutocompleteWidget) formfield = utils.autocomplete_formfield('testapp.customrender', view=autocomplete) self.assertIsInstance(formfield, forms.CharField) self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget) formfield = utils.autocomplete_formfield('testapp.email', view=autocomplete) self.assertIsInstance(formfield, forms.CharField) self.assertIsInstance(formfield.widget, widgets.AutocompleteWidget)
def autocomplete_formfield(self, ac_id, formfield=None, **kwargs): formfield = autocomplete_formfield(ac_id, formfield, self.autocomplete_view, AdminAutocompleteWidget, AdminMultipleAutocompleteWidget, **kwargs) if (self.autocomplete_view.settings[ac_id].add_button and isinstance(ac_id, (models.ForeignKey, models.ManyToManyField)) and formfield and ac_id.name not in self.raw_id_fields): request = kwargs.get("request", None) related_modeladmin = self.admin_site._registry.get( ac_id.rel.to) can_add_related = bool(related_modeladmin and related_modeladmin.has_add_permission(request)) if django.VERSION[1] > 2: # for django 1.3+ formfield.widget = admin.widgets.RelatedFieldWidgetWrapper( formfield.widget, ac_id.rel, self.admin_site, can_add_related=can_add_related) elif can_add_related: formfield.widget = admin.widgets.RelatedFieldWidgetWrapper( formfield.widget, ac_id.rel, self.admin_site) if not isinstance(ac_id, basestring): self._set_help_text(ac_id, formfield) return formfield
def autocomplete_formfield(self, ac_id, formfield=None, **kwargs): return autocomplete_formfield(ac_id, formfield, self.autocomplete_view, AdminAutocompleteWidget, AdminMultipleAutocompleteWidget, **kwargs)
def test_custom_formfield(self): formfield = utils.autocomplete_formfield('testapp.email', forms.EmailField, view=autocomplete) self.assertIsInstance(formfield, forms.EmailField)