Opened 6 years ago

Closed 5 years ago

#30421 closed New feature (fixed)

Allow ManyToManyField using a intermediary table to be defined as symmetrical.

Reported by: Nadege Owned by: Nadege
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: many2many, through, symmetrical, through
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Thanks to the work made by Collin Anderson in #9475 I think we can remove the check
"fields.E332 Many-to-many fields with intermediate tables must not be symmetrical." with a little adjustment.

This change was discussed in the django-dev mailing list https://groups.google.com/forum/#!topic/django-developers/BuT0-Uq8pyc.

This would let have

class Person(models.Model):
    name = models.CharField(max_length=20)
    friends = models.ManyToManyField('self', through='Friendship')

class Friendship(models.Model):
    first = models.ForeignKey(Person, models.CASCADE, related_name="+")
    second = models.ForeignKey(Person, models.CASCADE)
    friendship_date = models.DateTimeField()

and just do something like

joe.friends.add(anna, through_defaults={'friendship_date': date.datetime(...)})

where currently we would have to do

joe.friends.add(anna, through_defaults={'friendship_date': date.datetime(...)})
anna.friends.add(joe, through_defaults={'friendship_date': date.datetime(...)})

Change History (6)

comment:1 by Nadege, 6 years ago

Has patch: set

comment:3 by Mariusz Felisiak, 6 years ago

Summary: Allow ManyToMany using a intermediary table to be defined as symmetricalAllow ManyToManyField using a intermediary table to be defined as symmetrical.
Triage Stage: UnreviewedAccepted
Version: 2.2master

comment:4 by Mariusz Felisiak, 6 years ago

Patch needs improvement: set

comment:5 by Mariusz Felisiak, 5 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: assignedclosed

In 87b1ad6e:

Fixed #30421 -- Allowed symmetrical intermediate table for self-referential ManyToManyField.

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