1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.

This commit is contained in:
Nick Pope
2023-08-31 02:57:40 +01:00
committed by GitHub
parent 68a8996bdf
commit 500e01073a
29 changed files with 822 additions and 249 deletions

View File

@@ -199,7 +199,8 @@ class CharFieldTests(TestCase):
field.check(),
[
Error(
"'choices' must be an iterable (e.g., a list or tuple).",
"'choices' must be a mapping (e.g. a dictionary) or an iterable "
"(e.g. a list or tuple).",
obj=field,
id="fields.E004",
),
@@ -217,8 +218,9 @@ class CharFieldTests(TestCase):
field.check(),
[
Error(
"'choices' must be an iterable containing (actual value, "
"human readable name) tuples.",
"'choices' must be a mapping of actual values to human readable "
"names or an iterable containing (actual value, human readable "
"name) tuples.",
obj=field,
id="fields.E005",
),
@@ -260,8 +262,9 @@ class CharFieldTests(TestCase):
field.check(),
[
Error(
"'choices' must be an iterable containing (actual "
"value, human readable name) tuples.",
"'choices' must be a mapping of actual values to human "
"readable names or an iterable containing (actual value, "
"human readable name) tuples.",
obj=field,
id="fields.E005",
),
@@ -309,8 +312,9 @@ class CharFieldTests(TestCase):
field.check(),
[
Error(
"'choices' must be an iterable containing (actual value, "
"human readable name) tuples.",
"'choices' must be a mapping of actual values to human readable "
"names or an iterable containing (actual value, human readable "
"name) tuples.",
obj=field,
id="fields.E005",
),
@@ -337,8 +341,9 @@ class CharFieldTests(TestCase):
field.check(),
[
Error(
"'choices' must be an iterable containing (actual value, "
"human readable name) tuples.",
"'choices' must be a mapping of actual values to human readable "
"names or an iterable containing (actual value, human readable "
"name) tuples.",
obj=field,
id="fields.E005",
),
@@ -386,6 +391,26 @@ class CharFieldTests(TestCase):
],
)
def test_choices_callable(self):
def get_choices():
return [(i, i) for i in range(3)]
class Model(models.Model):
field = models.CharField(max_length=10, choices=get_choices)
field = Model._meta.get_field("field")
self.assertEqual(
field.check(),
[
Error(
"'choices' must be a mapping (e.g. a dictionary) or an iterable "
"(e.g. a list or tuple).",
obj=field,
id="fields.E004",
),
],
)
def test_bad_db_index_value(self):
class Model(models.Model):
field = models.CharField(max_length=10, db_index="bad")
@@ -854,7 +879,8 @@ class IntegerFieldTests(SimpleTestCase):
field.check(),
[
Error(
"'choices' must be an iterable (e.g., a list or tuple).",
"'choices' must be a mapping (e.g. a dictionary) or an iterable "
"(e.g. a list or tuple).",
obj=field,
id="fields.E004",
),
@@ -872,8 +898,9 @@ class IntegerFieldTests(SimpleTestCase):
field.check(),
[
Error(
"'choices' must be an iterable containing (actual value, human "
"readable name) tuples.",
"'choices' must be a mapping of actual values to human readable "
"names or an iterable containing (actual value, human readable "
"name) tuples.",
obj=field,
id="fields.E005",
),