#31512: Django TextChoices cannot utilize `is` or `==` isn't explicitly stated
-------------------------------------+-------------------------------------
Reporter: Casper Weiss Bang | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):
* status: new => closed
* resolution: => invalid
Old 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.
New 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 at least had documented how you could check for equality
with them.
The current solution seems, sadly, hacky at best.
--
Comment:
`==` 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/31512#comment:1>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.12009688578377c3d10693ac63d055b1%40djangoproject.com.