Document ModelForm specifying field class corresponding to model fields with choices.
If a model field has choices, Model.formfield
will disregard the form_class
keyword argument (plucked from ModelForm.Meta.field_classes
), in favor of choices_form_class
, which is not set by the ModelForm field generation machinery, and defaults to TypedChoiceField
. As a result, the following won't work:
class Language(models.Model):
LANGUAGES = (
('FR', 'French'),
('EN', 'English')
)
iso = models.CharField(max_length=3, choices=LANGUAGES)
class SloppyIsoField(CharField):
"""Choice field that strips and uppercases language codes"""
def to_python(self, value):
value = super().to_python(value)
return value.strip().upper()
class LanguageForm(forms.ModelForm):
class Meta:
fields = ('iso',)
field_classes = {
'iso': SloppyIsoField
}
Granted, this is a bit contrived, but it might be worth mentioning the caveat in the docs. Or maybe allow a Meta.choices_field_classes
? If this is relevant I'd happily contribute.
Change History
(6)
Component: |
Forms → Documentation
|
Triage Stage: |
Unreviewed → Accepted
|
Type: |
New feature → Cleanup/optimization
|
Version: |
2.2 → master
|
Summary: |
ModelForm: allow to declaratively specify field classes corresponding to model fields with choices → Document ModelForm specifying field class corresponding to model fields with choices.
|
Owner: |
changed from nobody to Tobias Kunze
|
Status: |
new → assigned
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
Hi Quentin,
Thanks for the report.
Essentially, you've hit the limit of what's worth doing with
Meta
options vs just declaring the form fields by hand. There comes a point when the meta API just isn't any simpler...— I don't think there'd be any appetite for adding another meta option for this. "Just declare the
iso = SloppyIsoField(...)
directly" will be the advise. (Or use a custom model field class specifying it'schoices_form_class
informfield
...)I guess an addition to Overriding the default fields section of the ModelForms docs would be OK, if you can come up with a suitable addition.
Looking from the paragraph starting
Finally, if you want complete control over of a field ...
. A sentence or two added there may be appropriate.(Will accept on that basis.)