Hi James, On Mon, 31 Dec 2018 10:24:39 -0800 James Bennett <ubernost...@gmail.com> wrote:
> The basic problem is that, to take the example in the ticket, if I > were to issue a request like "/students/?year=FR", and the view were > to read that "year" param and try to filter on it, it would fail -- > the string "FR" will not compare equal to the enum member "FR". So I > couldn't easily use that to filter for students whose year is > Student.YearInSchool.Freshman. Like so: >>> class SchoolYear(Enum): ... FRESHMAN = 'FR' ... JUNIOR = 'JR' ... >>> SchoolYear.FRESHMAN == 'FR' False > > > This is a deliberate design decision in Python's enums which, > unfortunately, makes them unsuitable for the kinds of things people > would commonly use them for in Django; only the special one-off > enum.IntEnum has members that actually are comparable to a base > Python type, and writing special-case enum variants for other types > to try to enable comparability would be a pain. > The actual definition of IntEnum is class IntEnum(int, Enum): """Enum where members are also (and must be) ints""" (it turns out that you don't need a 'pass' statement when you have a docstring). And indeed, >>> class SchoolYear(str, Enum): ... FRESHMAN = 'FR' ... JUNIOR = 'JR' ... >>> SchoolYear.FRESHMAN == 'FR' True All it takes is adding the base Python type as a base of your enumeration type. Shai. -- 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 post to this group, send email to firstname.lastname@example.org. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/20181231231946.1d973327.shai%40platonix.com. For more options, visit https://groups.google.com/d/optout.