Browse Source

Fixed #36061 -- Added migration support for ManyToManyField.through_fields.

Added through_fields support to ManyToManyField.deconstruct.
Thanks to Simon Charette for pointers and the review.
brian 2 tháng trước cách đây
mục cha
commit
b13b8684a0
3 tập tin đã thay đổi với 20 bổ sung0 xóa
  1. 1 0
      AUTHORS
  2. 2 0
      django/db/models/fields/related.py
  3. 17 0
      tests/field_deconstruction/tests.py

+ 1 - 0
AUTHORS

@@ -180,6 +180,7 @@ answer newbie questions, and generally made Django that much better:
     Brian Fabian Crain <http://www.bfc.do/>
     Brian Harring <ferringb@gmail.com>
     Brian Helba <brian.helba@kitware.com>
+    Brian Nettleton <bdnettleton@gmail.com>
     Brian Ray <http://brianray.chipy.org/>
     Brian Rosner <brosner@gmail.com>
     Bruce Kroeze <https://coderseye.com/>

+ 2 - 0
django/db/models/fields/related.py

@@ -1796,6 +1796,8 @@ class ManyToManyField(RelatedField):
                 kwargs["through"] = self.remote_field.through
             elif not self.remote_field.through._meta.auto_created:
                 kwargs["through"] = self.remote_field.through._meta.label
+        if through_fields := getattr(self.remote_field, "through_fields", None):
+            kwargs["through_fields"] = through_fields
         # If swappable is True, then see if we're actually pointing to the target
         # of a swap.
         swappable_setting = self.swappable_setting

+ 17 - 0
tests/field_deconstruction/tests.py

@@ -516,6 +516,23 @@ class FieldDeconstructionTests(SimpleTestCase):
         self.assertEqual(path, "django.db.models.ManyToManyField")
         self.assertEqual(args, [])
         self.assertEqual(kwargs, {"to": "auth.permission", "through": "auth.Group"})
+        # Test through_fields
+        field = models.ManyToManyField(
+            "auth.Permission",
+            through="auth.Group",
+            through_fields=("foo", "permissions"),
+        )
+        name, path, args, kwargs = field.deconstruct()
+        self.assertEqual(path, "django.db.models.ManyToManyField")
+        self.assertEqual(args, [])
+        self.assertEqual(
+            kwargs,
+            {
+                "to": "auth.permission",
+                "through": "auth.Group",
+                "through_fields": ("foo", "permissions"),
+            },
+        )
         # Test custom db_table
         field = models.ManyToManyField("auth.Permission", db_table="custom_table")
         name, path, args, kwargs = field.deconstruct()