Opened 5 years ago

Last modified 3 months ago

#17688 assigned Bug

No m2m_changed signal sent to when referenced object is deleted

Reported by: jblaine@… Owned by: jorgecarleitao
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: anssi.kaariainen@…, Derek Hohls, No-0n3, jorgecarleitao@…, bronger@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

class Topping(models.Model):
    name = models.CharField()

class Pizza(models.Model):
    name = models.CharField()
    toppings = models.ManyToManyField(Topping, null=true, blank=true)

And this data established using those models:

    TOPPING
    id=1, name="Pepperoni"
    id=2, name="Onion"
    id=3, name="Mushroom"

    PIZZA
    id=1, name="foopizza"
    toppings=1,2,3
  1. Deleting any Topping object (for example, deleting id=1, name="Pepperoni") also removes it from foopizza.toppings. GOOD
  1. No m2m_changed signal is sent when 1 (above happens). BAD?

Attachments (1)

17688.diff (620 bytes) - added by Anssi Kääriäinen 5 years ago.

Download all attachments as: .zip

Change History (14)

comment:1 Changed 5 years ago by Anssi Kääriäinen

Cc: anssi.kaariainen@… added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

I confirmed that no m2m_changed signal is sent. It seems it would be somewhat easy to send m2m_changed signals from models/deletion.py. I have no clue what the correct arguments for all the m2m_signal args should be.

The attached test shows that no signal is sent.

Changed 5 years ago by Anssi Kääriäinen

Attachment: 17688.diff added

comment:2 Changed 5 years ago by Anssi Kääriäinen

jblaine: could you (or somebody else) provide the details what arguments do you expect the signal to send in this case? (I am talking about the pk_list, model, action, ...) arguments. I guess the right answer is the same as what you would send in tobbing.pizza_set.clear() case. However maybe it would be better to send all the deleted toppings in one batch (which seems to be possible from the delete code), so that you could get "all the pizzas for which toppings were removed" more effectively.

I am not 100% sure if the delete code really allows easily sending the m2m_changed signal. But it seems like it.

comment:3 Changed 5 years ago by anonymous

Being pretty new, I don't know that I am qualified to say. I just expected it to work the same as what the following scenario would cause to happen:

from django.db.models.signals import m2m_changed

def some_callback(sender, **kwargs):
    # custom code

m2m_changed.connect(some_callback, sender=Pizza.toppings.through)
foopizza.toppings.remove(ToppingObjectHere)

Because, in effect, that's what is happening for each Pizza with a relationship to the Topping that was deleted.

I think anything other than that will cause a new special case to document ("Note: when deleting an object referenced by a ManyToManyField, m2m_changed works as follows...").

No?

comment:4 Changed 5 years ago by Derek Hohls

Cc: Derek Hohls added

comment:5 Changed 3 years ago by Jeff Blaine

As we only use Django for 1 internal site, are not Django-internals savvy, and I don't have the time to become Django-internals savvy, I have instead made a personal $25 donation to Django Project made in support of getting this straightened out properly in Django. That's all I can offer aside from the suggestion that this ticket among several others shows that, IMO, the overall signals stuff perhaps has not been looked at with a "Forget what's there for a second. What's the RIGHT thing to be doing in designing the signals stuff and addressing the various tickets that point out original design shortsightedness." mindset.

Thank you for your consideration.

comment:6 Changed 17 months ago by Abhaya Agarwal

#24738 closed as duplicate.

comment:7 Changed 17 months ago by No-0n3

Cc: No-0n3 added

Is there any plans on fixing this bug in the coming version?

comment:8 Changed 17 months ago by Tim Graham

Given the most recent activity was 2 years ago, it doesn't appear anyone is working on it.

comment:9 Changed 11 months ago by jorgecarleitao

Cc: jorgecarleitao@… added
Has patch: set
Owner: changed from nobody to jorgecarleitao
Status: newassigned

Created PR for this issue: https://github.com/django/django/pull/5505

I'm not convinced that using getattr and an import in the function is the best solution, but I didn't find a better option so far.

comment:10 Changed 8 months ago by Tim Graham

Patch needs improvement: set

Left some comments for improvement on the PR.

comment:11 Changed 5 months ago by Torsten Bronger

Cc: bronger@… added

comment:12 Changed 5 months ago by jorgecarleitao

Patch needs improvement: unset

New PR with suggestions from old PR implemented: https://github.com/django/django/pull/6579

comment:13 Changed 3 months ago by Tim Graham

Patch needs improvement: set

Left some more comments for improvement.

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