Opened 5 years ago
Last modified 14 months ago
#31262 closed New feature
Support dictionaries in Field.choices for named groups. — at Version 1
Reported by: | Tom Forbes | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Adam Johnson | 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 (last modified by )
The Django documentation gives this example for creating named groups of choices:
MEDIA_CHOICES = [ ('Audio', ( ('vinyl', 'Vinyl'), ('cd', 'CD'), ) ), ('Video', ( ('vhs', 'VHS Tape'), ('dvd', 'DVD'), ) ), ('unknown', 'Unknown'), ]
With Python 3.7 (and implicitly in CPython 3.6) dictionaries are ordered, meaning this syntax could be replaced by the cleaner and easier to parse:
{ "Audio": ( ('vinyl', 'Vinyl'), ('cd', 'CD'), ), "Video": ( ('vhs', 'VHS Tape'), ('dvd', 'DVD'), ), "unknown": "Unknown" }
Once 3.7 is the lowest supported version we should document that this is supported, and ensure that it works correctly.
Change History (1)
comment:1 by , 5 years ago
Component: | Forms → Database layer (models, ORM) |
---|---|
Description: | modified (diff) |
Summary: | Document and explicitly support dictionaries being passed to field choices → Support dictionaries in Field.choices for named groups. |
Triage Stage: | Unreviewed → Accepted |
Version: | → master |
Note:
See TracTickets
for help on using tickets.
Agreed, we can officially add this even in Django 3.1, because we've already made a decision to assume that dictionaries preserve ordering (see #30159 and a short discussion).