Opened 7 years ago

Closed 6 years ago

#17683 closed Bug (fixed)

`BaseModelFormSet` ignore form's widgets options when adding the pk field

Reported by: Simon Charette Owned by: Simon Charette
Component: Forms Version: master
Severity: Normal Keywords: basemodelformset widget
Cc: charette.s@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


When defining a custom widget for the pk field of a modelform used in a modelformset this way:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        widgets = {
          'id': forms.HiddenInput(attrs={'class': 'a-useful-class'})

MyFormSet = modelformset_factory(MyModel, MyModelForm)

The specified widget is ignored because of this line.

It should be replaced by something along

widget = form._meta.widgets.get(, HiddenInput)
form.fields[] = ModelChoiceField(qs, initial=pk_value, required=False, widget=widget)

I'll provide patch with tests if this gets accepted.

Attachments (1)

ticket-17683-basemodelformset-pk-widget-v1.diff (3.2 KB) - added by Simon Charette 7 years ago.
Fix with tests

Download all attachments as: .zip

Change History (7)

comment:1 Changed 7 years ago by Aymeric Augustin

Type: UncategorizedBug

comment:2 Changed 7 years ago by Claude Paroz

Needs tests: set
Triage Stage: UnreviewedAccepted

I see no reason why the pk field wouldn't respect the widgets dict. Patch welcome!

Changed 7 years ago by Simon Charette

Fix with tests

comment:3 Changed 7 years ago by Simon Charette

Cc: charette.s@… added
Has patch: set
Needs tests: unset

Added patch with tests. Note that I also corrected regressiontests.model_formsets.regress.CustomWidget (introduced for #13095) to actually be a widget and not a field subclass.

comment:4 Changed 7 years ago by Adrien Lemaire

Triage Stage: AcceptedReady for checkin


comment:5 Changed 6 years ago by Simon Charette

Owner: changed from nobody to Simon Charette
Status: newassigned

comment:6 Changed 6 years ago by Simon Charette <charette.s@…>

Resolution: fixed
Status: assignedclosed

In a097ee32d8364045a950d6a36b19630fc34397f1:

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

Note: See TracTickets for help on using tickets.
Back to Top