summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2019-10-23 16:14:06 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-10-25 09:19:56 +0200
commitdbcd7b064e7278614f29fc45468d461e263d4da7 (patch)
tree5ec620cf4635ccacacbfbeb2dc1dcc8ab6dd4778
parentfc2b1cc926e34041953738e58fa6ad3053059b22 (diff)
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.
-rw-r--r--django/db/models/enums.py8
-rw-r--r--tests/model_enums/tests.py6
2 files changed, 13 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):
diff --git a/tests/model_enums/tests.py b/tests/model_enums/tests.py
index 6b4bd6e7fd..e1810e673a 100644
--- a/tests/model_enums/tests.py
+++ b/tests/model_enums/tests.py
@@ -143,6 +143,12 @@ class ChoicesTests(SimpleTestCase):
APPLE = 1, 'Apple'
PINEAPPLE = 1, 'Pineapple'
+ def test_str(self):
+ for test in [Gender, Suit, YearInSchool, Vehicle]:
+ for member in test:
+ with self.subTest(member=member):
+ self.assertEqual(str(test[member.name]), str(member.value))
+
class Separator(bytes, models.Choices):
FS = b'\x1c', 'File Separator'