1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #17461 -- Doc'd the presumed order of foreign keys on intermediary M2M model.

This commit is contained in:
Clifford Gama 2024-11-03 16:32:55 +02:00
parent 611bf6c2e2
commit 1e1ab5876a
No known key found for this signature in database
GPG Key ID: BF895AA45E520E21
2 changed files with 35 additions and 6 deletions

View File

@ -2018,6 +2018,34 @@ that control how the relationship functions.
prefer Django not to create a backwards relation, set ``related_name`` prefer Django not to create a backwards relation, set ``related_name``
to ``'+'``. to ``'+'``.
.. admonition:: Order of foreign keys to source models in intermediary models
When defining an asymmetric many-to-many relationship from a model to
itself using an intermediary model, the first foreign key in the
intermediary model will be treated as representing the source side of
the ``ManyToManyField``, and the second as the target side. For
example::
from django.db import models
class Manufacturer(models.Model):
name = models.CharField(max_length=255)
clients = models.ManyToManyField(
"self", symmetrical=False, related_name="suppliers"
)
class Supply(models.Model):
supplier = models.ForeignKey(Manufacturer, models.CASCADE)
client = models.ForeignKey(Manufacturer, models.CASCADE)
product = models.CharField(max_length=255)
Here, the ``Manufacturer`` model in its role as a supplier defines
the many-to-many relationship with ``clients``, so the ``supplier``
foreign key must come before the ``clients`` foreign key in the
intermediary ``Supply`` model.
If you don't specify an explicit ``through`` model, there is still an If you don't specify an explicit ``through`` model, there is still an
implicit ``through`` model class you can use to directly access the table implicit ``through`` model class you can use to directly access the table
created to hold the association. It has three fields to link the models. created to hold the association. It has three fields to link the models.

View File

@ -518,12 +518,13 @@ There are a few restrictions on the intermediate model:
to the foreign key to the target model (this would be ``Person`` in our to the foreign key to the target model (this would be ``Person`` in our
example). example).
* For a model which has a many-to-many relationship to itself through an * For a model that has a many-to-many relationship to itself through an
intermediary model, two foreign keys to the same model are permitted, but intermediary model, two foreign keys to the same model are allowed, but they
they will be treated as the two (different) sides of the many-to-many will be treated as the two different sides of the many-to-many relationship.
relationship. If there are *more* than two foreign keys though, you The first foreign key in the intermediary model will be taken to represent
must also specify ``through_fields`` as above, or a validation error the source side of the ``ManyToManyField``, while the second will be taken to
will be raised. represent the target side. If more than two foreign keys are defined, you
must specify ``through_fields``, or a validation error will occur.
Now that you have set up your :class:`~django.db.models.ManyToManyField` to use Now that you have set up your :class:`~django.db.models.ManyToManyField` to use
your intermediary model (``Membership``, in this case), you're ready to start your intermediary model (``Membership``, in this case), you're ready to start