Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#13534 closed (invalid)

m2m_changed signal doesn't call receiver

Reported by: Chris Targett <chris@…> Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2-beta
Severity: Keywords: m2m_save, dispatch, signal
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

For some reason or another the m2m_changed signal does not call the receiver.

A rough models.py similar to my code:

class SomeContent(models.Model):
  sites = models.ForeignKey(Site)

def m2m_handler(sender, instance, action, reverse, model, pk_set, **kwargs):
  raise NotImplementedError
models.signals.m2m_changed.connect(m2m_handler, SomeContent)


>>> sc = SomeContent()
>>> sc.save()
>>> sc.sites.add(Site.objects.get_current()) # Here you would expect a "NotImplementedError"
>>>

It seems the signal loses all receivers in django.dispatch.dispatcher.Signal._line_receivers line 215.

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The sample code is invalid on two counts:

1) The relationship isn't a m2m, so of course it doesn't emit an m2m signal
2) The sender of a m2m signal is bound to the through model, not the model defining the relationship. Assuming the key to Site was a m2m, you would need to connect:

{{
models.signals.m2m_changed.connect(m2m_handler, SomeContent.sites.through)
}}

comment:2 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.