{{{entry.save()}}} applies changes to database, but
{{{entry.authors.set(old_authors)}}} doesn't.
--
Ticket URL: <https://code.djangoproject.com/ticket/33972>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => worksforme
Comment:
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/33972#comment:1>