#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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070182f9ea0195-a03f337c-4583-499b-9702-40e464a4620a-000000%40eu-central-1.amazonses.com.

Reply via email to