#31261: Add support for defining models.Choices fields with dict, tuple, set and
list values.
-------------------------------------+-------------------------------------
     Reporter:  Tom Forbes           |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * status:  new => closed
 * resolution:   => needsinfo


Old description:

> As per the discussion on this forum post:
> https://forum.djangoproject.com/t/moving-to-django-3-0s-field-choices-
> enumeration-types/970/8
>
> It would be good to support two fairly common use cases with Enums:
> defining groups of related values (groups), and defining grouping for use
> with specific widgets.
>
> One way to enable both of these would be to simply special-case enum
> values with `dict`, `tuple`, `set` and `list` values:
>

> {{{
> class MoonMissions(Enum):
>     APOLLO_10 = 1
>     APOLLO_11 = 2
>     EXPLORER_33 = 3
>
>     FAILED_MISSIONS = {EXPLORER_33}
>     SUCCESSFUL_MISSIONS = (APOLLO_10, APOLLO_11)
>
>     SUCCESS_GROUPING = {
>         "Failed": FAILED_MISSIONS,
>         "Successful": SUCCESSFUL_MISSIONS
>     }
>
>     COUNTRY_GROUPING = [
>          ["USA", [APOLLO_10, APOLLO_11, EXPLORER_33]]
>     ]
> }}}
>
> Now a user can do:
>

> {{{
> SomeModel.objects.filter(mission__in=MoonMissions.FAILED_MISSIONS)
> enum_form_field.value in MoonMissions.SUCCESSFUL_MISSIONS
> models.IntegerField(choices=MoonMissions.COUNTRY_GROUPING)
> }}}

New description:

 As per the discussion on this forum post:
 https://forum.djangoproject.com/t/moving-to-django-3-0s-field-choices-
 enumeration-types/970/8

 It would be good to support two fairly common use cases with Enums:
 defining groups of related values (groups), and defining grouping for use
 with specific widgets.

 One way to enable both of these would be to simply special-case enum
 values with `dict`, `tuple`, `set` and `list` values:


 {{{
 class MoonMissions(models.IntegerChoices):
     APOLLO_10 = 1
     APOLLO_11 = 2
     EXPLORER_33 = 3

     FAILED_MISSIONS = {EXPLORER_33}
     SUCCESSFUL_MISSIONS = (APOLLO_10, APOLLO_11)

     SUCCESS_GROUPING = {
         "Failed": FAILED_MISSIONS,
         "Successful": SUCCESSFUL_MISSIONS
     }

     COUNTRY_GROUPING = [
          ["USA", [APOLLO_10, APOLLO_11, EXPLORER_33]]
     ]
 }}}

 Now a user can do:


 {{{
 SomeModel.objects.filter(mission__in=MoonMissions.FAILED_MISSIONS)
 enum_form_field.value in MoonMissions.SUCCESSFUL_MISSIONS
 models.IntegerField(choices=MoonMissions.COUNTRY_GROUPING)
 }}}

--

Comment:

 Thanks for this ticket, however I'm not sure if that's feasible because we
 support subclasses of `Choices` for a data type other that `int` or `str`
 (see
 
[https://github.com/django/django/blob/master/tests/model_enums/tests.py#L171-L205
 examples]). This can be break backward compatibility. I would be very
 happy to evaluate a patch, but without breaking a current behavior.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31261#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.c89c22addf809198e69503a12a17195f%40djangoproject.com.

Reply via email to