summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororlnub123 <30984274+orlnub123@users.noreply.github.com>2018-03-22 16:05:31 +0300
committerTim Graham <timograham@gmail.com>2018-04-20 11:06:14 -0400
commit21420096c4db78ccb8f549a29d662cff870d363c (patch)
tree44f96fb9fde87f58cc240a986adfb970b534ad01
parente35004966bacbc9ba1fb10dd01edcfd874dad303 (diff)
Fixed #29247 -- Allowed blank model field choice to be defined in nested choices.
-rw-r--r--django/db/models/fields/__init__.py3
-rw-r--r--tests/model_fields/tests.py13
2 files changed, 14 insertions, 2 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 44052a7bc3..f21a6df7f7 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -816,8 +816,7 @@ class Field(RegisterLookupMixin):
if self.choices:
choices = list(self.choices)
if include_blank:
- named_groups = isinstance(choices[0][1], (list, tuple))
- blank_defined = not named_groups and any(choice in ('', None) for choice, __ in choices)
+ blank_defined = any(choice in ('', None) for choice, _ in self.flatchoices)
if not blank_defined:
choices = blank_choice + choices
return choices
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index fb0098a262..45f61a0034 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -140,6 +140,19 @@ class GetChoicesTests(SimpleTestCase):
f = models.CharField(choices=choices)
self.assertEqual(f.get_choices(include_blank=True), choices)
+ def test_blank_in_grouped_choices(self):
+ choices = [
+ ('f', 'Foo'),
+ ('b', 'Bar'),
+ ('Group', (
+ ('', 'No Preference'),
+ ('fg', 'Foo'),
+ ('bg', 'Bar'),
+ )),
+ ]
+ f = models.CharField(choices=choices)
+ self.assertEqual(f.get_choices(include_blank=True), choices)
+
def test_lazy_strings_not_evaluated(self):
lazy_func = lazy(lambda x: 0 / 0, int) # raises ZeroDivisionError if evaluated.
f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))])