diff options
| author | Florian Apolloner <florian@apolloner.eu> | 2014-07-25 19:20:00 +0200 |
|---|---|---|
| committer | Florian Apolloner <florian@apolloner.eu> | 2014-07-27 13:32:07 +0200 |
| commit | 99c2c917c3f859f2cc3fc1f7d5abb72730aa6c48 (patch) | |
| tree | def3a6f48f60c7339b89f036326ce505520866c8 | |
| parent | f8ec200671649282b9e62fb1a4dfc138fa4eb4fb (diff) | |
[1.7.x] Fixed #23098 -- Checked that lazy choices are not evaluated too soon
Thanks Matthieu Agopian for the report.
Backport of 2f73b527dda6683868fac2791f7f07ccb01ea0d9 from master.
| -rw-r--r-- | django/db/models/fields/__init__.py | 10 | ||||
| -rw-r--r-- | tests/model_fields/tests.py | 7 |
2 files changed, 13 insertions, 4 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 65d6fa2acb..ad6af70544 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -730,10 +730,12 @@ class Field(RegisterLookupMixin): """Returns choices with a default blank choices included, for use as SelectField choices for this field.""" blank_defined = False - for choice, __ in self.choices: - if choice in ('', None): - blank_defined = True - break + 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 + break first_choice = (blank_choice if include_blank and not blank_defined else []) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index b718986fd2..0c38d28ba5 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -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'))) |
