#33972 closed Bug (worksforme)

Copying model instances of many-to-many field doesn't apply changes to database

Reported by: Eido Askayo Owned by: nobody
Component: Documentation Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In section Copying model instances, the following code is shown:

entry = Entry.objects.all()[0] # some previous entry
old_authors = entry.authors.all()
entry.pk = None
entry._state.adding = True
entry.save()
entry.authors.set(old_authors)

entry.save() applies changes to database, but entry.authors.set(old_authors) doesn't.

Change History (1)

comment:1 by Claude Paroz, 20 months ago

Resolution: worksforme
Status: newclosed

This passing test shows it does:

diff --git a/tests/many_to_many/tests.py b/tests/many_to_many/tests.py
index 53e870ddad..11e4a7c9a5 100644
--- a/tests/many_to_many/tests.py
+++ b/tests/many_to_many/tests.py
@@ -536,3 +536,23 @@ class ManyToManyTests(TestCase):
         self.assertEqual(
             self.p3.article_set.exists(), self.p3.article_set.all().exists()
         )
+
+    def test_copy_instance_with_m2m(self):
+        """
+        Copy an instance with m2m related instances.
+        Replicate example of:
+        https://docs.djangoproject.com/en/stable/topics/db/queries/#copying-model-instances
+        """
+        article = self.a2
+        old_pk = article.pk
+        old_pubs = article.publications.all()
+        old_pubs_pks = set(article.publications.all().values_list('pk', flat=True))
+        article.pk = None
+        article._state.adding = True
+        article.save()
+        article.publications.set(old_pubs)
+        self.assertNotEqual(old_pk, article.pk)
+        self.assertEqual(
+            old_pubs_pks,
+            set(article.publications.all().values_list('pk', flat=True)),
+        )

You probably need to get some help from support channels.

Note: See TracTickets for help on using tickets.
Back to Top