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')