Skip to content

Commit

Permalink
Refs #24561 -- Ensured callable choices are deconstructed as the actu…
Browse files Browse the repository at this point in the history
…al function.
  • Loading branch information
nessita committed Aug 25, 2023
1 parent b5d7a33 commit 60896c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -631,6 +631,8 @@ def deconstruct(self):
equals_comparison = {"choices", "validators"}
for name, default in possibles.items():
value = getattr(self, attr_overrides.get(name, name))
if isinstance(value, CallableChoiceIterator):
value = value.choices_func
# Do correct kind of comparison
if name in equals_comparison:
if value != default:
Expand Down
10 changes: 10 additions & 0 deletions tests/field_deconstruction/tests.py
Expand Up @@ -113,6 +113,16 @@ def test_choices_iterable(self):
self.assertEqual(args, [])
self.assertEqual(kwargs, {"choices": normalize_choices("012345")})

def test_choices_callable(self):
def get_choices():
return [(i, str(i)) for i in range(3)]

field = models.IntegerField(choices=get_choices)
name, path, args, kwargs = field.deconstruct()
self.assertEqual(path, "django.db.models.IntegerField")
self.assertEqual(args, [])
self.assertEqual(kwargs, {"choices": get_choices})

def test_csi_field(self):
field = models.CommaSeparatedIntegerField(max_length=100)
name, path, args, kwargs = field.deconstruct()
Expand Down

0 comments on commit 60896c9

Please sign in to comment.