Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32310 closed Cleanup/optimization (fixed)

Clarify ManyToManyField.through note about reverse accessors names for non-symmetrical recursive relationships.

Reported by: Fabio Sangiovanni Owned by: Fabio Sangiovanni
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Hello and happy new year everyone!

A note in the current ManyToManyField.through docs (https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through) suggests that:

Recursive relationships using an intermediary model and defined as symmetrical (that is, with symmetrical=True, which is the default) can’t determine the reverse accessors names, as they would be the same.

But the value of the symmetrical argument seems irrelevant in this regard, since in both cases (True or False value) the system checks framework raises the same error (fields.E304).

Repro:

from django.db import models


class Relationship(models.Model):
    person1 = models.ForeignKey('Person', on_delete=models.CASCADE)
    person2 = models.ForeignKey('Person', on_delete=models.CASCADE)


class Person(models.Model):
    other = models.ManyToManyField('self', through=Relationship, symmetrical=False)

manage.py makemigrations yields:

SystemCheckError: System check identified some issues:

ERRORS:
app.Relationship.person1: (fields.E304) Reverse accessor for 'Relationship.person1' clashes with reverse accessor for 'Relationship.person2'.
	HINT: Add or change a related_name argument to the definition for 'Relationship.person1' or 'Relationship.person2'.
app.Relationship.person2: (fields.E304) Reverse accessor for 'Relationship.person2' clashes with reverse accessor for 'Relationship.person1'.
	HINT: Add or change a related_name argument to the definition for 'Relationship.person2' or 'Relationship.person1'.

for both values of the symmetrical argument of ManyToManyField.

Am I missing something?

Thanks!

Change History (6)

comment:1 by Mariusz Felisiak, 4 years ago

Easy pickings: set
Summary: ManyToManyField.through docs clarificationClarify ManyToManyField.through note about reverse accessors names for non-symmetrical recursive relationships.
Triage Stage: UnreviewedAccepted

Good catch. IMO we can chop: "and defined as symmetrical (that is, with symmetrical=True, which is the default)". Would you like to prepare a patch?

comment:2 by Fabio Sangiovanni, 4 years ago

Sure, I'm happy to open a PR for this. Will do as soon as I can.

comment:3 by Fabio Sangiovanni, 4 years ago

Owner: changed from nobody to Fabio Sangiovanni
Status: newassigned

comment:4 by Fabio Sangiovanni, 4 years ago

Has patch: set

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 2d6c9b9:

Fixed #32310 -- Fixed note about reverse accessors for intermediate table for self-referential ManyToManyField.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 6d3a623:

[3.1.x] Fixed #32310 -- Fixed note about reverse accessors for intermediate table for self-referential ManyToManyField.

Backport of 2d6c9b97bc706aab1975f57e814461e90e389bb0 from master

Note: See TracTickets for help on using tickets.
Back to Top