Author: russellm
Date: 2011-08-22 21:08:24 -0700 (Mon, 22 Aug 2011)
New Revision: 16659

Modified:
   django/trunk/django/forms/models.py
   django/trunk/tests/regressiontests/model_forms_regress/tests.py
Log:
Fixed #15315 -- Added support for the 'widget' argument to modelform_factory. 
Thanks to SardarNL and Will Hardy for the patch.

Modified: django/trunk/django/forms/models.py
===================================================================
--- django/trunk/django/forms/models.py 2011-08-23 04:05:47 UTC (rev 16658)
+++ django/trunk/django/forms/models.py 2011-08-23 04:08:24 UTC (rev 16659)
@@ -368,7 +368,7 @@
     __metaclass__ = ModelFormMetaclass
 
 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
     # construct a ModelForm without creating and passing in a temporary
     # inner class.
@@ -379,6 +379,8 @@
         attrs['fields'] = fields
     if exclude is not None:
         attrs['exclude'] = exclude
+    if widgets is not None:
+        attrs['widgets'] = widgets
 
     # If parent form class already has an inner Meta, the Meta we're
     # creating needs to inherit from the parent's inner meta.

Modified: django/trunk/tests/regressiontests/model_forms_regress/tests.py
===================================================================
--- django/trunk/tests/regressiontests/model_forms_regress/tests.py     
2011-08-23 04:05:47 UTC (rev 16658)
+++ django/trunk/tests/regressiontests/model_forms_regress/tests.py     
2011-08-23 04:08:24 UTC (rev 16659)
@@ -275,6 +275,20 @@
         Form = modelform_factory(Person, form=BaseForm)
         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):
         """Test that a custom formfield_callback is used if provided"""
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to