Fixed #29247 -- Allowed blank model field choice to be defined in nested choices.
This commit is contained in:
parent
e35004966b
commit
21420096c4
|
@ -816,8 +816,7 @@ class Field(RegisterLookupMixin):
|
||||||
if self.choices:
|
if self.choices:
|
||||||
choices = list(self.choices)
|
choices = list(self.choices)
|
||||||
if include_blank:
|
if include_blank:
|
||||||
named_groups = isinstance(choices[0][1], (list, tuple))
|
blank_defined = any(choice in ('', None) for choice, _ in self.flatchoices)
|
||||||
blank_defined = not named_groups and any(choice in ('', None) for choice, __ in choices)
|
|
||||||
if not blank_defined:
|
if not blank_defined:
|
||||||
choices = blank_choice + choices
|
choices = blank_choice + choices
|
||||||
return choices
|
return choices
|
||||||
|
|
|
@ -140,6 +140,19 @@ class GetChoicesTests(SimpleTestCase):
|
||||||
f = models.CharField(choices=choices)
|
f = models.CharField(choices=choices)
|
||||||
self.assertEqual(f.get_choices(include_blank=True), 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):
|
def test_lazy_strings_not_evaluated(self):
|
||||||
lazy_func = lazy(lambda x: 0 / 0, int) # raises ZeroDivisionError if evaluated.
|
lazy_func = lazy(lambda x: 0 / 0, int) # raises ZeroDivisionError if evaluated.
|
||||||
f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))])
|
f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))])
|
||||||
|
|
Loading…
Reference in New Issue