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

@@ -31,6 +31,10 @@ from django.utils.translation import gettext_lazy as _
from .models import FoodManager, FoodQuerySet
def get_choices():
return [(i, str(i)) for i in range(3)]
class DeconstructibleInstances:
def deconstruct(self):
return ("DeconstructibleInstances", [], {})
@@ -493,6 +497,14 @@ class WriterTests(SimpleTestCase):
"models.IntegerField(choices=[('Group', [(2, '2'), (1, '1')])])",
)
def test_serialize_callable_choices(self):
field = models.IntegerField(choices=get_choices)
string = MigrationWriter.serialize(field)[0]
self.assertEqual(
string,
"models.IntegerField(choices=migrations.test_writer.get_choices)",
)
def test_serialize_nested_class(self):
for nested_cls in [self.NestedEnum, self.NestedChoices]:
cls_name = nested_cls.__name__