diff options
| author | Clifford Gama <cliffygamy@gmail.com> | 2025-02-10 09:52:27 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-03-04 13:07:08 +0100 |
| commit | 4406ce15ff487eb0ac5f384dd2ed84a8d7e40971 (patch) | |
| tree | d1666a3e546b4631db8c6a4198aa05f4a52135ec /docs/ref | |
| parent | 5997fdc9214eafaaf2fadc065f1d2ced34641598 (diff) | |
[5.2.x] Fixed #36128 -- Clarified auto-generated unique constraint on m2m through models.
Backport of ae2736ca3bf4c6a27e23ee95530ad965b550d4cc from main.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/contrib/admin/index.txt | 7 | ||||
| -rw-r--r-- | docs/ref/models/fields.txt | 11 |
2 files changed, 10 insertions, 8 deletions
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 1b02b7d403..4f459838f2 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2629,6 +2629,13 @@ we can do this with inline admin models. Suppose we have the following models:: date_joined = models.DateField() 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 define an inline class for the ``Membership`` model:: diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 52cf0b8286..c45688184d 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -2030,13 +2030,6 @@ that control how the relationship functions. .. 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:: - Recursive relationships using an intermediary model can't determine the reverse accessors names, as they would be the same. You need to set a :attr:`~ForeignKey.related_name` to at least one of them. If you'd @@ -2045,7 +2038,9 @@ that control how the relationship functions. 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 - 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 generated: |
