Ticket #15315: ticket15315.patch

File ticket15315.patch, 2.1 KB (added by willhardy, 4 years ago)

Updated the diff to the current trunk and added a test

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index b34f4d0..db9f059 100644
    a b class ModelForm(BaseModelForm): 
    368368    __metaclass__ = ModelFormMetaclass
    369369
    370370def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
    371                        formfield_callback=None):
     371                       widgets=None, formfield_callback=None):
    372372    # Create the inner Meta class. FIXME: ideally, we should be able to
    373373    # construct a ModelForm without creating and passing in a temporary
    374374    # inner class.
    def modelform_factory(model, form=ModelForm, fields=None, exclude=None, 
    379379        attrs['fields'] = fields
    380380    if exclude is not None:
    381381        attrs['exclude'] = exclude
     382    if widgets is not None:
     383        attrs['widgets'] = widgets
    382384
    383385    # If parent form class already has an inner Meta, the Meta we're
    384386    # creating needs to inherit from the parent's inner meta.
  • tests/regressiontests/model_forms_regress/tests.py

    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    index 9817858..5fdcee9 100644
    a b class FormFieldCallbackTests(TestCase): 
    275275        Form = modelform_factory(Person, form=BaseForm)
    276276        self.assertTrue(Form.base_fields['name'].widget is widget)
    277277
     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        assert Form.base_fields['name'].widget is not widget
     287
     288        # With a widget should not set the widget to textarea
     289        Form = modelform_factory(Person, widgets={'name':widget})
     290        self.assertTrue(Form.base_fields['name'].widget is widget)
     291
    278292    def test_custom_callback(self):
    279293        """Test that a custom formfield_callback is used if provided"""
    280294
Back to Top