1
0
mirror of https://github.com/django/django.git synced 2025-03-12 18:30:48 +00:00

[5.2.x] Fixed #36128 -- Clarified auto-generated unique constraint on m2m through models.

Backport of ae2736ca3bf4c6a27e23ee95530ad965b550d4cc from main.
This commit is contained in:
Clifford Gama 2025-02-10 09:52:27 +02:00 committed by Sarah Boyce
parent 5997fdc921
commit 4406ce15ff
3 changed files with 22 additions and 8 deletions

View File

@ -2629,6 +2629,13 @@ we can do this with inline admin models. Suppose we have the following models::
date_joined = models.DateField() date_joined = models.DateField()
invite_reason = models.CharField(max_length=64) invite_reason = models.CharField(max_length=64)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["person", "group"], name="unique_person_group"
)
]
The first step in displaying this intermediate model in the admin is to The first step in displaying this intermediate model in the admin is to
define an inline class for the ``Membership`` model:: define an inline class for the ``Membership`` model::

View File

@ -2028,13 +2028,6 @@ that control how the relationship functions.
:ref:`extra data with a many-to-many relationship :ref:`extra data with a many-to-many relationship
<intermediary-manytomany>`. <intermediary-manytomany>`.
.. note::
If you don't want multiple associations between the same instances, add
a :class:`~django.db.models.UniqueConstraint` including the from and to
fields. Django's automatically generated many-to-many tables include
such a constraint.
.. note:: .. note::
Recursive relationships using an intermediary model can't determine the Recursive relationships using an intermediary model can't determine the
@ -2045,7 +2038,9 @@ that control how the relationship functions.
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, a
primary key and two foreign keys. There is a unique constraint on the two
foreign keys.
If the source and target models differ, the following fields are If the source and target models differ, the following fields are
generated: generated:

View File

@ -503,10 +503,22 @@ something like this::
date_joined = models.DateField() date_joined = models.DateField()
invite_reason = models.CharField(max_length=64) invite_reason = models.CharField(max_length=64)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["person", "group"], name="unique_person_group"
)
]
When you set up the intermediary model, you explicitly specify foreign When you set up the intermediary model, you explicitly specify foreign
keys to the models that are involved in the many-to-many relationship. This keys to the models that are involved in the many-to-many relationship. This
explicit declaration defines how the two models are related. explicit declaration defines how the two models are related.
If you don't want multiple associations between the same instances, add a
:class:`~django.db.models.UniqueConstraint` including the ``from`` and ``to``
fields. Django's automatically generated many-to-many tables include such a
constraint.
There are a few restrictions on the intermediate model: There are a few restrictions on the intermediate model:
* Your intermediate model must contain one - and *only* one - foreign key * Your intermediate model must contain one - and *only* one - foreign key