Opened 9 years ago

Closed 9 years ago

#25672 closed Cleanup/optimization (fixed)

Clarify why related ManyToMany fields with a custom intermediary model disable the remove() method.

Reported by: Antoine Auberger Owned by: nobody
Component: Documentation Version: 1.8
Severity: Normal Keywords: manytomany related
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

It is currently impossible to add or create related item to a many-to-many relationship with custom intermediary model, with reason, as the custom model extra fields won't be populated.
This is documented and explained.

However, it is also not possible to remove related items from the collection, and the only reason given in the doc is : "The remove() method is disabled for similar reasons".

I've try to find a justification for this though the code/docs/history but I couldn't and this seems unjustified for me.
All the informations needed to remove the relation are provided if you just use this method e.g. mysourceobject.relatedobjects.remove(targetobject).
I don't see why it is disabled, and therefore would like to ask for a design change here, or an explanation.

Attachments (2)

25672.diff (2.8 KB ) - added by Tim Graham 9 years ago.
25672Docs.diff (1.4 KB ) - added by Benjamin Phillips 9 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by Tim Graham, 9 years ago

Russ called out the remove() method in ticket:6095#comment:33 but I fail to understand that reasoning as well. There doesn't seem to be any problem modify the relevant tests (patch attached - would need doc updates too). Are we missing something?

by Tim Graham, 9 years ago

Attachment: 25672.diff added

comment:2 by Baptiste Mispelon, 9 years ago

I had a chat with Russ and he raised the following point. Consider a custome through table that (unlike the auto-generated one) doesn't enforce unicity on the (model1, model2) pair (which is a valid usecase).

Something like this:

class Pizza(models.Model):
    name = models.CharField(max_length=50)


class Customer(models.Model):
    name = models.CharField(max_length=50)
    orders = models.ManyToManyField('Pizza', through='Order')


class Order(models.Model):
    pizza = models.ForeignKey('Pizza')
    customer = models.ForeignKey('Customer')
    ordered_on = models.DateTimeField(default=timezone.now)

Doing some_customer.orders.remove(some_pizza) doesn't give enough information as to which Order instance should actually be deleted.

It seems to me that we should just better document the reason behind this, considering it's not exactly obvious.

comment:3 by Tim Graham, 9 years ago

Component: Database layer (models, ORM)Documentation
Easy pickings: set
Summary: Related ManyToMany fields with custom intermediary model should not disable remove method.Clarify why related ManyToMany fields with a custom intermediary model disable the remove() method.
Triage Stage: UnreviewedAccepted

Thanks Baptiste!

by Benjamin Phillips, 9 years ago

Attachment: 25672Docs.diff added

comment:4 by Benjamin Phillips, 9 years ago

Took a stab at a documentation patch to clarify this issue.

comment:5 by Tim Graham, 9 years ago

Has patch: set

comment:6 by Benjamin Phillips, 9 years ago

Made a PR with for my patch (PR 5728 https://github.com/django/django/pull/5728)

comment:7 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In d508326:

Fixed #25672 -- Clarified why related ManyToManyFields with a custom intermediate model disable the remove() method.

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