Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit a5cbb892f8206c8f4ae8fd1bcff93b8251e6264e 1 parent 1f770c6
Russell Keith-Magee authored
4  django/forms/models.py
@@ -368,7 +368,7 @@ class ModelForm(BaseModelForm):
368 368
     __metaclass__ = ModelFormMetaclass
369 369
 
370 370
 def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
371  
-                       formfield_callback=None):
  371
+                      formfield_callback=None,  widgets=None):
372 372
     # Create the inner Meta class. FIXME: ideally, we should be able to
373 373
     # construct a ModelForm without creating and passing in a temporary
374 374
     # inner class.
@@ -379,6 +379,8 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
379 379
         attrs['fields'] = fields
380 380
     if exclude is not None:
381 381
         attrs['exclude'] = exclude
  382
+    if widgets is not None:
  383
+        attrs['widgets'] = widgets
382 384
 
383 385
     # If parent form class already has an inner Meta, the Meta we're
384 386
     # creating needs to inherit from the parent's inner meta.
14  tests/regressiontests/model_forms_regress/tests.py
@@ -275,6 +275,20 @@ class Meta:
275 275
         Form = modelform_factory(Person, form=BaseForm)
276 276
         self.assertTrue(Form.base_fields['name'].widget is widget)
277 277
 
  278
+    def test_factory_with_widget_argument(self):
  279
+        """ Regression for #15315: modelform_factory should accept widgets
  280
+            argument
  281
+        """
  282
+        widget = forms.Textarea()
  283
+
  284
+        # Without a widget should not set the widget to textarea
  285
+        Form = modelform_factory(Person)
  286
+        self.assertNotEqual(Form.base_fields['name'].widget.__class__, forms.Textarea)
  287
+
  288
+        # With a widget should not set the widget to textarea
  289
+        Form = modelform_factory(Person, widgets={'name':widget})
  290
+        self.assertEqual(Form.base_fields['name'].widget.__class__, forms.Textarea)
  291
+
278 292
     def test_custom_callback(self):
279 293
         """Test that a custom formfield_callback is used if provided"""
280 294
 

0 notes on commit a5cbb89

Please sign in to comment.
Something went wrong with that request. Please try again.