Hello all,
In my project I recently wanted to group choices, and have gotten used to the Enumeration Types Django provides. I noticed Django currently does not support named groups for these.
So, I've implemented them in two ways:
Option A) Ugly, but simple changes to Django
class Media(models.TextChoices):
_named_group1 = "Audio"
VINYL = "vinyl"
CD = "cd", "CD"
_named_group2 = "Video"
VHS_TAPE = "vhs", "VHS Tape"
DVD = "dvd", _("DVD")
_named_group3 = None
UNKNOWN = "unknown"
Option B) Pretty, but complex changes to Django
class Media(models.TextChoices):
class Audio(models.TextChoices):
VINYL = "vinyl"
CD = "cd", "CD"
class Video(models.TextChoices):
VHS_TAPE = "vhs", "VHS Tape"
DVD = "dvd", _("DVD")
UNKNOWN = "unknown"
Does anyone have better ideas to implement named groups here? Would one of these implementations be useful for Django?
Below are links to personal pull requests to see potential patches. The pull requests include tests and documentation.
GitHub links:
Relevant docs:
Thank you,
Steven H Johnson