summaryrefslogtreecommitdiff
path: root/django/db/models/enums.py
AgeCommit message (Collapse)Author
2025-01-20Fixed #36005 -- Dropped support for Python 3.10 and 3.11.Mariusz Felisiak
2025-01-15Removed ChoicesMeta per deprecation timeline.Sarah Boyce
2023-09-13Modernized enumeration helpers on Python 3.11+.Nick Pope
- use @enum.property https://docs.python.org/3/library/enum.html#enum.property - use @enum.nonmember Using @property on an enum class does not yield the expected result. do_not_call_in_templates attribute works because a @property instance is truthy. We can make this a literal True value as expected by using @enum.nonmember in Python 3.11+. https://docs.python.org/3/library/enum.html#enum.nonmember - used enum.IntEnum/StrEnum Python 3.11+ has ReprEnum which uses int.__str__() and str.__str__() for __str__() in the `IntEnum` and `StrEnum` subclasses. We can emulate that for Python < 3.11. https://docs.python.org/3/library/enum.html#enum.ReprEnum https://docs.python.org/3/library/enum.html#enum.IntEnum https://docs.python.org/3/library/enum.html#enum.StrEnum
2023-09-13Refs #34233 -- Used @staticmethod with TextChoices._generate_next_value_().Nick Pope
Now that Python 3.10 is the minimum supported version, we can decorate _generate_next_value_() with @staticmethod. It wasn't possible before as Python < 3.10 does not support calling static methods direct from the class body. https://docs.python.org/3/library/enum.html#enum.Enum._generate_next_value_
2023-09-12Renamed ChoicesMeta to ChoicesType.Nick Pope
This also uses enum.EnumType for Python 3.11+ as Python 3.11 renamed EnumMeta to EnumType. While the former is still available as an alias of the latter for now, let's prefer the canonical name for this. Check out https://docs.python.org/3/library/enum.html#enum.EnumType
2023-09-12Removed ChoicesMeta.__contains__() for Python 3.12+.Nick Pope
In Python 3.12 it is possible to check containment using member values, not just the members themselves. https://docs.python.org/3/library/enum.html#enum.EnumType.__contains__
2022-02-07Refs #33476 -- Reformatted code with Black.django-bot
2021-07-12Refs #32074 -- Used Enum.repr() format proposed for Python 3.10.Mariusz Felisiak
The Python's Steering Council decided to revert changes in the Enum module (see https://bugs.python.org/issue44559) and moved them to Python 3.11. Follow up to 5d9b065d3f93de056588dfee6f1776294dd8bab2. Thanks Nick Pope for the review.
2021-04-07Refs #32074 -- Backported Enum.__repr__() from Python 3.10.Mariusz Felisiak
Enum.__repr__() was changed in [1], we should use the same format in Python < 3.10. [1] https://bugs.python.org/issue40066
2021-03-24Fixed #32460 -- Allowed "label"/"do_not_call_in_templates" members in model ↵Nick Pope
choice enums.
2021-02-04Refs #32074 -- Fixed TextChoices/IntegerChoices crash on Python 3.10.Mariusz Felisiak
EnumMeta has a new keyword argument 'boundary' in Python 3.10. This is a new mechanism that controls how out-of-range / invalid bits are handled, see https://bugs.python.org/issue38250.
2020-01-10Fixed #31154 -- Added support for using enumeration types in templates.Adam Johnson
Enumeration helpers are callables, so the template system tried to call them with no arguments. Thanks Rupert Baker for helping discover this.
2019-12-16Optimized containment check in ChoicesMeta.Ram Rachum
2019-10-25Fixed #30902 -- Added __str__() for model choice enums.Carlton Gibson
Allows expected behavior when cast to str, also matching behaviour of created instances with those fetched from the DB. Thanks to Simon Charette, Nick Pope, and Shai Berger for reviews.
2019-09-04Fixed #27910 -- Added enumeration helpers for use in Field.choices.Shai Berger
These classes can serve as a base class for user enums, supporting translatable human-readable names, or names automatically inferred from the enum member name. Additional properties make it easy to access the list of names, values and display labels. Thanks to the following for ideas and reviews: Carlton Gibson, Fran Hrženjak, Ian Foote, Mariusz Felisiak, Shai Berger. Co-authored-by: Shai Berger <shai@platonix.com> Co-authored-by: Nick Pope <nick.pope@flightdataservices.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>