Opened 11 years ago
Closed 11 years ago
#24295 closed New feature (fixed)
Allow specifying form field types in a ModelForm's Meta
| Reported by: | Loic Bistuer | Owned by: | Loic Bistuer |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
It's currently very fiddly to use a custom form field together with ModelForm.
You can either set the field declaratively, which requires redefining all the attributes that would be automatically pulled from the model. It is not very DRY and quite error-prone, especially when it comes to required / blank. Or you can abuse models.Field.formfield by doing: my_field = MyModel._meta.get_field('my_field').formfield(form_class=MyCustomField).
This ticket proposes allowing:
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter', 'slug']
field_classes = {
'slug': MySlugFormField,
}
Change History (4)
comment:1 by , 11 years ago
| Has patch: | set |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:2 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
I think this is worth doing. It addresses a common need for which the existing solutions are all either ugly or error-prone.
comment:3 by , 11 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:4 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
https://github.com/django/django/pull/4072