From 4d2f9c38e835383fe42548353890ed4a302eaa58 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 14 Aug 2014 19:47:02 -0700 Subject: [PATCH] Fixed #23288: deconstruct() ignoring related_name --- django/db/models/fields/related.py | 14 ++++++++++++-- tests/field_deconstruction/tests.py | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index be12079f50..97ca395b35 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1383,6 +1383,14 @@ class ForeignObject(RelatedField): name, path, args, kwargs = super(ForeignObject, self).deconstruct() kwargs['from_fields'] = self.from_fields kwargs['to_fields'] = self.to_fields + if self.rel.related_name is not None: + kwargs['related_name'] = self.rel.related_name + if self.rel.related_query_name is not None: + kwargs['related_query_name'] = self.rel.related_query_name + if self.rel.on_delete != CASCADE: + kwargs['on_delete'] = self.rel.on_delete + if self.rel.parent_link: + kwargs['parent_link'] = self.rel.parent_link # Work out string form of "to" if isinstance(self.rel.to, six.string_types): kwargs['to'] = self.rel.to @@ -1670,8 +1678,6 @@ class ForeignKey(ForeignObject): kwargs['db_index'] = False if self.db_constraint is not True: kwargs['db_constraint'] = self.db_constraint - if self.rel.on_delete is not CASCADE: - kwargs['on_delete'] = self.rel.on_delete # Rel needs more work. to_meta = getattr(self.rel.to, "_meta", None) if self.rel.field_name and (not to_meta or (to_meta.pk and self.rel.field_name != to_meta.pk.name)): @@ -2142,6 +2148,10 @@ class ManyToManyField(RelatedField): kwargs['db_table'] = self.db_table if self.rel.db_constraint is not True: kwargs['db_constraint'] = self.rel.db_constraint + if self.rel.related_name is not None: + kwargs['related_name'] = self.rel.related_name + if self.rel.related_query_name is not None: + kwargs['related_query_name'] = self.rel.related_query_name # Rel needs more work. if isinstance(self.rel.to, six.string_types): kwargs['to'] = self.rel.to diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index c5f3f63999..6253c8e61b 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -203,6 +203,12 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.ForeignKey") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission", "to_field": "foobar"}) + # Test related_name preservation + field = models.ForeignKey("auth.Permission", related_name="foobar") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.ForeignKey") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "related_name": "foobar"}) @override_settings(AUTH_USER_MODEL="auth.Permission") def test_foreign_key_swapped(self): @@ -277,6 +283,12 @@ class FieldDeconstructionTests(TestCase): self.assertEqual(path, "django.db.models.ManyToManyField") self.assertEqual(args, []) self.assertEqual(kwargs, {"to": "auth.Permission", "db_table": "custom_table"}) + # Test related_name + field = models.ManyToManyField("auth.Permission", related_name="custom_table") + name, path, args, kwargs = field.deconstruct() + self.assertEqual(path, "django.db.models.ManyToManyField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"to": "auth.Permission", "related_name": "custom_table"}) @override_settings(AUTH_USER_MODEL="auth.Permission") def test_many_to_many_field_swapped(self):