Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17683 -- Make sure `BaseModelFormSet` respects defined widgets.

  • Loading branch information...
commit a097ee32d8364045a950d6a36b19630fc34397f1 1 parent ec469ad
@charettes charettes authored
View
6 django/forms/models.py
@@ -678,7 +678,11 @@ def pk_is_not_editable(pk):
else:
qs = self.model._default_manager.get_query_set()
qs = qs.using(form.instance._state.db)
- form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
+ if form._meta.widgets:
+ widget = form._meta.widgets.get(self._pk_field.name, HiddenInput)
+ else:
+ widget = HiddenInput
+ form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=widget)
super(BaseModelFormSet, self).add_fields(form, index)
def modelformset_factory(model, form=ModelForm, formfield_callback=None,
View
17 tests/regressiontests/model_formsets_regress/tests.py
@@ -261,14 +261,17 @@ def test_extraneous_query_is_not_run(self):
formset.save()
-class CustomWidget(forms.CharField):
+class CustomWidget(forms.widgets.TextInput):
pass
class UserSiteForm(forms.ModelForm):
class Meta:
model = UserSite
- widgets = {'data': CustomWidget}
+ widgets = {
+ 'id': CustomWidget,
+ 'data': CustomWidget,
+ }
class Callback(object):
@@ -283,24 +286,27 @@ def __call__(self, db_field, **kwargs):
class FormfieldCallbackTests(TestCase):
"""
- Regression for #13095: Using base forms with widgets
- defined in Meta should not raise errors.
+ Regression for #13095 and #17683: Using base forms with widgets
+ defined in Meta should not raise errors and BaseModelForm should respect
+ the specified pk widget.
"""
def test_inlineformset_factory_default(self):
Formset = inlineformset_factory(User, UserSite, form=UserSiteForm)
form = Formset().forms[0]
+ self.assertTrue(isinstance(form['id'].field.widget, CustomWidget))
self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))
def test_modelformset_factory_default(self):
Formset = modelformset_factory(UserSite, form=UserSiteForm)
form = Formset().forms[0]
+ self.assertTrue(isinstance(form['id'].field.widget, CustomWidget))
self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))
def assertCallbackCalled(self, callback):
id_field, user_field, data_field = UserSite._meta.fields
expected_log = [
- (id_field, {}),
+ (id_field, {'widget': CustomWidget}),
(user_field, {}),
(data_field, {'widget': CustomWidget}),
]
@@ -318,7 +324,6 @@ def test_modelformset_custom_callback(self):
formfield_callback=callback)
self.assertCallbackCalled(callback)
-
class BaseCustomDeleteFormSet(BaseFormSet):
"""
A formset mix-in that lets a form decide if it's to be deleted.
Please sign in to comment.
Something went wrong with that request. Please try again.