1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +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

@@ -318,3 +318,18 @@ class ValidationTests(SimpleTestCase):
f.clean("A", None)
with self.assertRaises(ValidationError):
f.clean("3", None)
def test_callable_choices(self):
def get_choices():
return {i: str(i) for i in range(3)}
f = models.IntegerField(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)