Loading django/db/models/fields/__init__.py +6 −4 Original line number Diff line number Diff line Loading @@ -730,6 +730,8 @@ class Field(RegisterLookupMixin): """Returns choices with a default blank choices included, for use as SelectField choices for this field.""" blank_defined = False named_groups = self.choices and isinstance(self.choices[0][1], (list, tuple)) if not named_groups: for choice, __ in self.choices: if choice in ('', None): blank_defined = True Loading tests/model_fields/tests.py +7 −0 Original line number Diff line number Diff line Loading @@ -416,6 +416,13 @@ class ValidationTest(test.TestCase): f = models.CharField(choices=[('', '<><>'), ('a', 'A')]) self.assertEqual(f.get_choices(True), [('', '<><>'), ('a', 'A')]) def test_charfield_get_choices_doesnt_evaluate_lazy_strings(self): # Regression test for #23098 # Will raise ZeroDivisionError if lazy is evaluated lazy_func = lazy(lambda x: 0/0, int) f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))]) self.assertEqual(f.get_choices(True)[0], ('', '---------')) def test_choices_validation_supports_named_groups(self): f = models.IntegerField( choices=(('group', ((10, 'A'), (20, 'B'))), (30, 'C'))) Loading Loading
django/db/models/fields/__init__.py +6 −4 Original line number Diff line number Diff line Loading @@ -730,6 +730,8 @@ class Field(RegisterLookupMixin): """Returns choices with a default blank choices included, for use as SelectField choices for this field.""" blank_defined = False named_groups = self.choices and isinstance(self.choices[0][1], (list, tuple)) if not named_groups: for choice, __ in self.choices: if choice in ('', None): blank_defined = True Loading
tests/model_fields/tests.py +7 −0 Original line number Diff line number Diff line Loading @@ -416,6 +416,13 @@ class ValidationTest(test.TestCase): f = models.CharField(choices=[('', '<><>'), ('a', 'A')]) self.assertEqual(f.get_choices(True), [('', '<><>'), ('a', 'A')]) def test_charfield_get_choices_doesnt_evaluate_lazy_strings(self): # Regression test for #23098 # Will raise ZeroDivisionError if lazy is evaluated lazy_func = lazy(lambda x: 0/0, int) f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))]) self.assertEqual(f.get_choices(True)[0], ('', '---------')) def test_choices_validation_supports_named_groups(self): f = models.IntegerField( choices=(('group', ((10, 'A'), (20, 'B'))), (30, 'C'))) Loading