Opened 2 years ago

Closed 23 months ago

#20465 closed New feature (needsinfo)

ModelAdmin.formfield_overrides should look at field attributes

Reported by: zuber Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

ModelAdmin.formfield_overrides should look at field attributes in addition to field class, when choosing which widget to use in template. The most important use case here is the choices attribute, as it's likely that user will want to render fields with choices differently (i.e. as select) than fields without choices (which can be a normal input).

After #19303 is merged, current mechanism will allow users to override widget choice for all fields with a certain class. It won't be possible to choose different widget for normal fields and fields with choices, short of overriding the ModelForm. That's not optimal.

Change History (2)

comment:1 Changed 2 years ago by DrMeers

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

As stated in the / docs, formfield_overrides "provides a quick-and-dirty way to override some of the Field options for use in the admin". If you want to do something more complicated than a simple per-field-class override, you should look into overriding ModelAdmin.formfield_for_dbfield. Whilst not overly elegant, it will allow you to completely customise the form fields based on whatever properties you like without "overriding the ModelForm". You may also be interested in / ModelAdmin.formfield_for_choice_field in this instance.

Feel free to propose details of a more elegant solution, though please bear in mind backwards-compatibility concerns. You may also find the details and outcome of #17924 relevant, involving a similar proposal for ModelForms in general.

Last edited 2 years ago by DrMeers (previous) (diff)

comment:2 Changed 23 months ago by timo

  • Resolution set to needsinfo
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top