Unverified Kaydet (Commit) c03e4171 authored tarafından Tim Graham's avatar Tim Graham Kaydeden (comit) GitHub

Refs #28748 -- Reallowed lazy model field choices.

Regression in 3aa9ab39.
üst 3dffcb55
...@@ -244,10 +244,10 @@ class Field(RegisterLookupMixin): ...@@ -244,10 +244,10 @@ class Field(RegisterLookupMixin):
if not self.choices: if not self.choices:
return [] return []
def is_value(value): def is_value(value, accept_promise=True):
return isinstance(value, (str, Promise)) or not is_iterable(value) return isinstance(value, (str, Promise) if accept_promise else str) or not is_iterable(value)
if is_value(self.choices): if is_value(self.choices, accept_promise=False):
return [ return [
checks.Error( checks.Error(
"'choices' must be an iterable (e.g., a list or tuple).", "'choices' must be an iterable (e.g., a list or tuple).",
......
...@@ -4,6 +4,7 @@ from django.core.checks import Error, Warning as DjangoWarning ...@@ -4,6 +4,7 @@ from django.core.checks import Error, Warning as DjangoWarning
from django.db import connection, models from django.db import connection, models
from django.test import SimpleTestCase, TestCase, skipIfDBFeature from django.test import SimpleTestCase, TestCase, skipIfDBFeature
from django.test.utils import isolate_apps, override_settings from django.test.utils import isolate_apps, override_settings
from django.utils.functional import lazy
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
...@@ -188,6 +189,12 @@ class CharFieldTests(TestCase): ...@@ -188,6 +189,12 @@ class CharFieldTests(TestCase):
self.assertEqual(Model._meta.get_field('field').check(), []) self.assertEqual(Model._meta.get_field('field').check(), [])
def test_lazy_choices(self):
class Model(models.Model):
field = models.CharField(max_length=10, choices=lazy(lambda: [[1, '1'], [2, '2']], tuple)())
self.assertEqual(Model._meta.get_field('field').check(), [])
def test_choices_named_group(self): def test_choices_named_group(self):
class Model(models.Model): class Model(models.Model):
field = models.CharField( field = models.CharField(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment