File tree 3 files changed +22
-8
lines changed 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -2635,6 +2635,13 @@ we can do this with inline admin models. Suppose we have the following models::
2635
2635
date_joined = models.DateField()
2636
2636
invite_reason = models.CharField(max_length=64)
2637
2637
2638
+ class Meta:
2639
+ constraints = [
2640
+ models.UniqueConstraint(
2641
+ fields=["person", "group"], name="unique_person_group"
2642
+ )
2643
+ ]
2644
+
2638
2645
The first step in displaying this intermediate model in the admin is to
2639
2646
define an inline class for the ``Membership`` model::
2640
2647
Original file line number Diff line number Diff line change @@ -2009,13 +2009,6 @@ that control how the relationship functions.
2009
2009
:ref:`extra data with a many-to-many relationship
2010
2010
<intermediary-manytomany>`.
2011
2011
2012
- .. note::
2013
-
2014
- If you don't want multiple associations between the same instances, add
2015
- a :class:`~django.db.models.UniqueConstraint` including the from and to
2016
- fields. Django's automatically generated many-to-many tables include
2017
- such a constraint.
2018
-
2019
2012
.. note::
2020
2013
2021
2014
Recursive relationships using an intermediary model can't determine the
@@ -2026,7 +2019,9 @@ that control how the relationship functions.
2026
2019
2027
2020
If you don't specify an explicit ``through`` model, there is still an
2028
2021
implicit ``through`` model class you can use to directly access the table
2029
- created to hold the association. It has three fields to link the models.
2022
+ created to hold the association. It has three fields to link the models, a
2023
+ primary key and two foreign keys. There is a unique constraint on the two
2024
+ foreign keys.
2030
2025
2031
2026
If the source and target models differ, the following fields are
2032
2027
generated:
Original file line number Diff line number Diff line change @@ -507,10 +507,22 @@ something like this::
507
507
date_joined = models.DateField()
508
508
invite_reason = models.CharField(max_length=64)
509
509
510
+ class Meta:
511
+ constraints = [
512
+ models.UniqueConstraint(
513
+ fields=["person", "group"], name="unique_person_group"
514
+ )
515
+ ]
516
+
510
517
When you set up the intermediary model, you explicitly specify foreign
511
518
keys to the models that are involved in the many-to-many relationship. This
512
519
explicit declaration defines how the two models are related.
513
520
521
+ If you don't want multiple associations between the same instances, add a
522
+ :class:`~django.db.models.UniqueConstraint` including the ``from`` and ``to``
523
+ fields. Django's automatically generated many-to-many tables include such a
524
+ constraint.
525
+
514
526
There are a few restrictions on the intermediate model:
515
527
516
528
* Your intermediate model must contain one - and *only* one - foreign key
You can’t perform that action at this time.
0 commit comments