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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user