[Django] #33972: Copying model instances of many-to-many field doesn't apply changes to database

16 views
Skip to first unread message

Django

unread,
Sep 1, 2022, 8:56:41 AM9/1/22
to django-...@googlegroups.com
#33972: Copying model instances of many-to-many field doesn't apply changes to
database
-----------------------------------------+------------------------
Reporter: Eido Askayo | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
In section [https://docs.djangoproject.com/en/4.0/topics/db/queries
/#copying-model-instances 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33972>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 1, 2022, 12:37:21 PM9/1/22
to django-...@googlegroups.com
#33972: Copying model instances of many-to-many field doesn't apply changes to
database
-------------------------------+--------------------------------------

Reporter: Eido Askayo | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 4.0
Severity: Normal | Resolution: worksforme

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Claude Paroz):

* 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>

Reply all
Reply to author
Forward
0 new messages