Opened 14 years ago
Closed 13 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)
Change History (7)
comment:1 by , 14 years ago
| Type: | Uncategorized → Bug |
|---|
comment:2 by , 14 years ago
| Needs tests: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 14 years ago
| Cc: | 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:5 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:6 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
I see no reason why the pk field wouldn't respect the widgets dict. Patch welcome!