Opened 10 months ago

Last modified 7 months ago

#27704 new New feature

contrib.postgres.ArrayField with choices should use TypedMultipleChoiceField as its default form field class

Reported by: Carl Meyer Owned by:
Component: contrib.postgres Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Currently the contrib.postgres ArrayField implements no special handling for choices, which makes it pretty useless with choices; you can specify your choices as a list of possible arrays for the ArrayField to contain. This is, I suppose, consistent in some technical sense, but rarely if ever useful in practice. It's very handy to use an ArrayField with choices to implement a multiple-choice field. We already have a TypedMultipleChoiceField form field in Django, so the implementation for this is just a trivial enhancement to the ArrayField.formfield method: if self.choices is set, use TypedMultipleChoiceField as the form class, with the base_field used to determine the expected type.

Any objections to this before I submit a PR for it?

Change History (4)

comment:1 Changed 10 months ago by Claude Paroz

Triage Stage: UnreviewedAccepted

Please!

comment:2 Changed 9 months ago by Claude Paroz

See also #27161

comment:3 Changed 9 months ago by Claude Paroz

Has patch: set
Patch needs improvement: set

Here is a preliminary patch. Obviously needs some more work, but it's a start...

comment:4 Changed 7 months ago by Claude Paroz

I've updated the patch and clarified the fact that choices should be defined on the base_field, not on the ArrayField itself. Hopefully it's a sensible recommendation!

Note: See TracTickets for help on using tickets.
Back to Top