#27910: Use Enum class in model choices -------------------------------------+------------------------------------- Reporter: Marcel | Owner: nobody Hellwig | Type: New | Status: new feature | Component: Database | Version: 1.10 layer (models, ORM) | Severity: Normal | Keywords: enum model choices Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- I will simply stick to your example here: https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
I want to limit the input to certain choices so I create a list/tuple of tuples/lists. Since Python 3.4 there is a class called [https://docs.python.org/3.5/library/enum.html Enum] and some nice decorators like [https://docs.python.org/3.5/library/enum.html#ensuring- unique-enumeration-values @unique]. Currently, if you want to use this you have to do 1/2 dirty hacks, descriped in [http://blog.richard.do/index.php/2014/02/how-to-use-enums- for-django-field-choices/ this article], which I would prefer over the current solution. It would be nice to have native support for the Enum class, e.g. you can directly pass the class to choices (instead of using the class method choices() ), also when refering to an element in the enum just using {{{Student.Freshmann}}} instead of {{{Student.Freshmann.value}}} A simple example would be this: {{{ from enum import Enum class Student(models.Model): class YearInSchoolChoices(Enum): Freshman = 'FR' Sophomore = 'SO' Junior = 'JR' Senior = 'SR' year_in_school = models.CharField( max_length=2, choices=YearInSchoolChoices, default=YearInSchoolChoices.Freshman, ) def is_upperclass(self): return self.year_in_school in (self.YearInSchoolChoices.Junior, self.YearInSchoolChoices.Senior) }}} Also this could be adopted (if accepted) to any type of choices in django, e.g. [https://docs.djangoproject.com/en/1.10/ref/forms/fields/#choicefield Choicefield] etc. -- Ticket URL: <https://code.djangoproject.com/ticket/27910> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/051.858b8cca42b350f7166abed98e9acc9c%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.