Opened 12 years ago

Closed 11 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: dev
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

Description

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(self._pk_field.name, HiddenInput)
form.fields[self._pk_field.name] = 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 12 years ago.
Fix with tests

Download all attachments as: .zip

Change History (7)

comment:1 by Aymeric Augustin, 12 years ago

Type: UncategorizedBug

comment:2 by Claude Paroz, 12 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

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

by Simon Charette, 12 years ago

Fix with tests

comment:3 by Simon Charette, 12 years ago

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 by Adrien Lemaire, 12 years ago

Triage Stage: AcceptedReady for checkin

Good

comment:5 by Simon Charette, 11 years ago

Owner: changed from nobody to Simon Charette
Status: newassigned

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

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