Fixed #15315 -- Added support for the 'widget' argument to modelform_factory. Thanks to SardarNL and Will Hardy for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16659 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1f770c62da
commit
a5cbb892f8
|
@ -368,7 +368,7 @@ class ModelForm(BaseModelForm):
|
||||||
__metaclass__ = ModelFormMetaclass
|
__metaclass__ = ModelFormMetaclass
|
||||||
|
|
||||||
def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
|
def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
|
||||||
formfield_callback=None):
|
formfield_callback=None, widgets=None):
|
||||||
# Create the inner Meta class. FIXME: ideally, we should be able to
|
# Create the inner Meta class. FIXME: ideally, we should be able to
|
||||||
# construct a ModelForm without creating and passing in a temporary
|
# construct a ModelForm without creating and passing in a temporary
|
||||||
# inner class.
|
# inner class.
|
||||||
|
@ -379,6 +379,8 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
|
||||||
attrs['fields'] = fields
|
attrs['fields'] = fields
|
||||||
if exclude is not None:
|
if exclude is not None:
|
||||||
attrs['exclude'] = exclude
|
attrs['exclude'] = exclude
|
||||||
|
if widgets is not None:
|
||||||
|
attrs['widgets'] = widgets
|
||||||
|
|
||||||
# If parent form class already has an inner Meta, the Meta we're
|
# If parent form class already has an inner Meta, the Meta we're
|
||||||
# creating needs to inherit from the parent's inner meta.
|
# creating needs to inherit from the parent's inner meta.
|
||||||
|
|
|
@ -275,6 +275,20 @@ class FormFieldCallbackTests(TestCase):
|
||||||
Form = modelform_factory(Person, form=BaseForm)
|
Form = modelform_factory(Person, form=BaseForm)
|
||||||
self.assertTrue(Form.base_fields['name'].widget is widget)
|
self.assertTrue(Form.base_fields['name'].widget is widget)
|
||||||
|
|
||||||
|
def test_factory_with_widget_argument(self):
|
||||||
|
""" Regression for #15315: modelform_factory should accept widgets
|
||||||
|
argument
|
||||||
|
"""
|
||||||
|
widget = forms.Textarea()
|
||||||
|
|
||||||
|
# Without a widget should not set the widget to textarea
|
||||||
|
Form = modelform_factory(Person)
|
||||||
|
self.assertNotEqual(Form.base_fields['name'].widget.__class__, forms.Textarea)
|
||||||
|
|
||||||
|
# With a widget should not set the widget to textarea
|
||||||
|
Form = modelform_factory(Person, widgets={'name':widget})
|
||||||
|
self.assertEqual(Form.base_fields['name'].widget.__class__, forms.Textarea)
|
||||||
|
|
||||||
def test_custom_callback(self):
|
def test_custom_callback(self):
|
||||||
"""Test that a custom formfield_callback is used if provided"""
|
"""Test that a custom formfield_callback is used if provided"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue