Opened 4 years ago

Closed 4 years ago

#31512 closed Bug (invalid)

Django TextChoices cannot utilize `is` or `==` isn't explicitly stated

Reported by: Casper Weiss Bang Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

i was reading about the "new" enums feature here:

https://docs.djangoproject.com/en/3.0/ref/models/fields/#enumeration-types

And wanted to utilize it. However i tried to use is to check for equality rather than in. That made it impossible to utilize before i realized that the returned type from the CharField is, naturally, a char, and that i could check for equality with in: self.year_in_school in self.YearInSchool.JUNIOR

This probably isn't the most beautiful thing though, and it wasn't really stated that this is the defacto way to check for equality (lest i use self.year_in_school in self.YearInSchool.JUNIOR.value.

It would be quite nice if the self.year_in_school type actually was of type ENUM, or at least had documented how you could check for equality with them.

The current solution seems, sadly, hacky at best.

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Resolution: invalid
Status: newclosed

== works properly for me, e.g.

class Event(models.TextChoices):
    C = 'Carnival!'
    F = 'Festival!'

>>> p1 = Post.objects.create(title=Event.C)
>>> p1.title == Event.C
True

I wouldn't expect that is will work here.

The current solution seems, sadly, hacky at best.

Please try to be more polite and appreciate the effort of contributors.

Note: See TracTickets for help on using tickets.
Back to Top