#27910: Use Enum class in model choices
-------------------------------------+-------------------------------------
     Reporter:  Marcel Hellwig       |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  enum model choices   |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 > Or you could swap the positions.

 That would work for text based fields (`CharField`, `TextField`) but what
 about choices defined for a integer based field?

 I feel like ''choices'' are a different concept than enums and that while
 the later is useful for referring to choices values as constants, choices
 and label mapping should be defined independently.

 {{{#!python
 class StudentType(enum.Enum):
     Freshman = 'FR'
     Sophomore = 'SO'
     Junior = 'JR'
     Senior = 'SR'

 class Student(models.Model):
     type = models.CharField(choices=[
         (StudentType.Freshman, _('Freshman')),
         (StudentType.Sophomore, _('Sophomore')),
         (StudentType.Junior, _('Junior')),
         (StudentType.Senior, _('Senior')),
     ])
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27910#comment:3>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.d8fe43203920c5d71584f5ea5c4b1ddf%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to