1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #24561 -- Added support for callables on model fields' choices.

This commit is contained in:
Natalia
2023-08-31 09:09:30 -03:00
parent 5bfb3cbf49
commit 691f70c477
9 changed files with 101 additions and 31 deletions

View File

@@ -89,3 +89,18 @@ class ValidationTests(SimpleTestCase):
msg = "This field cannot be null."
with self.assertRaisesMessage(ValidationError, msg):
f.clean(None, None)
def test_callable_choices(self):
def get_choices():
return {str(i): f"Option {i}" for i in range(3)}
f = models.CharField(max_length=1, choices=get_choices)
for i in get_choices():
with self.subTest(i=i):
self.assertEqual(i, f.clean(i, None))
with self.assertRaises(ValidationError):
f.clean("A", None)
with self.assertRaises(ValidationError):
f.clean("3", None)