#25518: Multiple ManyToManyField related_name documentation
---------------------------------+--------------------------------------
     Reporter:  brandonchinn178  |      Owner:  nobody
         Type:  Uncategorized    |     Status:  new
    Component:  Documentation    |    Version:  1.8
     Severity:  Normal           |   Keywords:  manytomanyfield multiple
 Triage Stage:  Unreviewed       |  Has patch:  0
Easy pickings:  0                |      UI/UX:  0
---------------------------------+--------------------------------------
 I came across a bug [http://stackoverflow.com/questions/32957347/django-
 manytomany-field-add-not-working the other day] about having a model with
 multiple `ManyToManyFields` to the same `Model`. The bug occurs with the
 following code:

 {{{
 # models.py
 class MyModel(models.Model):
     foo = models.ManyToManyField('other_app.OtherModel', related_name='+')
     bar = models.ManyToManyField('other_app.OtherModel', related_name='+')

 # shell
 >>> x = MyModel.objects.create()
 >>> y = OtherModel.objects.create()
 >>> x.foo.add(y)
 >>> x.foo.all() # returns []
 }}}

 So there's no errors or anything thrown. However, accessing with the
 through model still works.

 {{{
 >>> x.foo.through.objects.all() # returns [<MyModel_othermodel object>]
 }}}

 I eventually found the answer in the
 
[https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.ManyToManyField.related_name
 Django documentation for version 1.6] saying that if you have more than
 one `ManyToManyField` pointing to the same model, you need to set the
 `related_name` to a '''''unique value''''' ending in `+`. However, this
 description was removed starting in the 1.7 docs. I think we need to re-
 add this in because it's a pretty significant specification about the
 `ManyToManyField` I would never have found, if not for a
 [http://stackoverflow.com/questions/13918968/multiple-many-to-many-
 relations-to-the-same-model-in-django 2012 StackOverflow post].

--
Ticket URL: <https://code.djangoproject.com/ticket/25518>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/058.a0e5b67eeb93559c833c276e768345c9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to