Opened 5 years ago
Last modified 5 years ago
#31512 closed Bug
Django TextChoices cannot utilize `is` or `==` isn't explicitly stated — at Initial Version
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
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 atleast had documented how you could check for equality with them.
The current solution seems, sadly, hacky at best.