diff options
| author | Carlton Gibson <carlton.gibson@noumenal.es> | 2019-10-23 16:14:06 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-10-25 09:38:41 +0200 |
| commit | 8740ff334ae3e001514c42ac1fb63a5e4cfa5cd1 (patch) | |
| tree | c6f7969a37401cd587f8ff31f2c238611e26d70d /django | |
| parent | 495cdd6add3abc37e2ff4e40c7df7e02eea4ce11 (diff) | |
[3.0.x] Fixed #30902 -- Added __str__() for model choice enums.
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.
Backport of dbcd7b064e7278614f29fc45468d461e263d4da7 from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/enums.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/django/db/models/enums.py b/django/db/models/enums.py index bbe362a6ab..ae20ef6d93 100644 --- a/django/db/models/enums.py +++ b/django/db/models/enums.py @@ -60,7 +60,13 @@ class ChoicesMeta(enum.EnumMeta): class Choices(enum.Enum, metaclass=ChoicesMeta): """Class for creating enumerated choices.""" - pass + + def __str__(self): + """ + Use value when cast to str, so that Choices set as model instance + attributes are rendered as expected in templates and similar contexts. + """ + return str(self.value) class IntegerChoices(int, Choices): |
