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:
* https://github.com/shjohnson-pi/django/pull/1 (Pretty, complex)
* https://github.com/shjohnson-pi/django/pull/2 (Ugly, simple)
Relevant docs:
* 
https://docs.djangoproject.com/en/4.1/ref/models/fields/#field-choices-named-groups
* https://docs.djangoproject.com/en/4.1/ref/models/fields/#enumeration-types

Thank you,
Steven H Johnson

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/7d66f6a9-4056-4e2e-ade2-8175994ebd20n%40googlegroups.com.
  • Nam... Steven Johnson
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)

Reply via email to