From 3aa9ab39cce6b2a27d6334ad0148c8f37b6f5986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Wed, 24 Jan 2018 20:21:13 -0800 Subject: [PATCH] Refs #28748 -- Reallowed lazy values in model field choices. Regression in f9844f484186fa13399bf8b96f58616687fe158a. Thanks Matthias Kestenholz for the report and suggestions. --- django/db/models/fields/__init__.py | 2 +- .../test_ordinary_fields.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index b511b01d17..efd4c09d2f 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -245,7 +245,7 @@ class Field(RegisterLookupMixin): return [] def is_value(value): - return isinstance(value, str) or not is_iterable(value) + return isinstance(value, (str, Promise)) or not is_iterable(value) if is_value(self.choices): return [ diff --git a/tests/invalid_models_tests/test_ordinary_fields.py b/tests/invalid_models_tests/test_ordinary_fields.py index 75e6148f98..281779828d 100644 --- a/tests/invalid_models_tests/test_ordinary_fields.py +++ b/tests/invalid_models_tests/test_ordinary_fields.py @@ -5,6 +5,7 @@ from django.db import connection, models from django.test import SimpleTestCase, TestCase, skipIfDBFeature from django.test.utils import isolate_apps, override_settings from django.utils.timezone import now +from django.utils.translation import gettext_lazy as _ @isolate_apps('invalid_models_tests') @@ -198,6 +199,12 @@ class CharFieldTests(TestCase): ), ]) + def test_choices_containing_lazy(self): + class Model(models.Model): + field = models.CharField(max_length=10, choices=[['1', _('1')], ['2', _('2')]]) + + self.assertEqual(Model._meta.get_field('field').check(), []) + def test_choices_named_group(self): class Model(models.Model): field = models.CharField( @@ -248,6 +255,17 @@ class CharFieldTests(TestCase): ), ]) + def test_choices_named_group_lazy(self): + class Model(models.Model): + field = models.CharField( + max_length=10, choices=[ + [_('knights'), [['L', _('Lancelot')], ['G', _('Galahad')]]], + ['R', _('Random character')], + ], + ) + + self.assertEqual(Model._meta.get_field('field').check(), []) + def test_bad_db_index_value(self): class Model(models.Model): field = models.CharField(max_length=10, db_index='bad')