Made nested deconstruction support both forms of deconstruct()
Nested deconstruction should (silently) handle Field.deconstruct() as well as other arbitrary deconstructable objects. This allows having a field in the deconstruction of another field.
This commit is contained in:
parent
604162604b
commit
6ab67919ac
|
@ -67,20 +67,21 @@ class MigrationAutodetector(object):
|
||||||
if not model._meta.proxy and model._meta.managed and al not in self.to_state.real_apps:
|
if not model._meta.proxy and model._meta.managed and al not in self.to_state.real_apps:
|
||||||
new_model_keys.append((al, mn))
|
new_model_keys.append((al, mn))
|
||||||
|
|
||||||
def _deep_deconstruct(obj, field=True):
|
def _deep_deconstruct(obj):
|
||||||
"""
|
"""
|
||||||
Recursive deconstruction for a field and its arguments.
|
Recursive deconstruction for a field and its arguments.
|
||||||
"""
|
"""
|
||||||
if not hasattr(obj, 'deconstruct'):
|
if not hasattr(obj, 'deconstruct'):
|
||||||
return obj
|
return obj
|
||||||
deconstructed = obj.deconstruct()
|
deconstructed = obj.deconstruct()
|
||||||
if field:
|
if isinstance(obj, models.Field):
|
||||||
|
# we have a field which also returns a name
|
||||||
deconstructed = deconstructed[1:]
|
deconstructed = deconstructed[1:]
|
||||||
name, args, kwargs = deconstructed
|
name, args, kwargs = deconstructed
|
||||||
return (
|
return (
|
||||||
name,
|
name,
|
||||||
[_deep_deconstruct(value, field=False) for value in args],
|
[_deep_deconstruct(value) for value in args],
|
||||||
dict([(key, _deep_deconstruct(value, field=False))
|
dict([(key, _deep_deconstruct(value))
|
||||||
for key, value in kwargs.items()])
|
for key, value in kwargs.items()])
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@ class AutodetectorTests(TestCase):
|
||||||
author_name_default = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default='Ada Lovelace'))])
|
author_name_default = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default='Ada Lovelace'))])
|
||||||
author_name_deconstructable_1 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
author_name_deconstructable_1 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
||||||
author_name_deconstructable_2 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
author_name_deconstructable_2 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
||||||
|
author_name_deconstructable_3 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=models.IntegerField()))])
|
||||||
|
author_name_deconstructable_4 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=models.IntegerField()))])
|
||||||
author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
||||||
author_renamed_with_book = ModelState("testapp", "Writer", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
author_renamed_with_book = ModelState("testapp", "Writer", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
||||||
author_with_publisher_string = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher_name", models.CharField(max_length=200))])
|
author_with_publisher_string = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher_name", models.CharField(max_length=200))])
|
||||||
|
@ -576,6 +578,16 @@ class AutodetectorTests(TestCase):
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
self.assertEqual(changes, {})
|
self.assertEqual(changes, {})
|
||||||
|
|
||||||
|
def test_deconstruct_field_kwarg(self):
|
||||||
|
"""
|
||||||
|
Field instances are handled correctly by nested deconstruction.
|
||||||
|
"""
|
||||||
|
before = self.make_project_state([self.author_name_deconstructable_3])
|
||||||
|
after = self.make_project_state([self.author_name_deconstructable_4])
|
||||||
|
autodetector = MigrationAutodetector(before, after)
|
||||||
|
changes = autodetector._detect_changes()
|
||||||
|
self.assertEqual(changes, {})
|
||||||
|
|
||||||
def test_replace_string_with_foreignkey(self):
|
def test_replace_string_with_foreignkey(self):
|
||||||
"""
|
"""
|
||||||
Adding an FK in the same "spot" as a deleted CharField should work. (#22300).
|
Adding an FK in the same "spot" as a deleted CharField should work. (#22300).
|
||||||
|
|
Loading…
Reference in New Issue