Opened 10 years ago

Closed 9 years ago

#6778 closed (duplicate)

dispatch signal needed on many to many relationship alterations (add, delete)

Reported by: xore.ander@… Owned by: Andrew Gibson
Component: Core (Other) Version: master
Severity: Keywords: dispatcher relation m2m
Cc: George Song, Christian Schilling, rvdrijst Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:


When a model has a relation added, no signal is sent.

This breaks functionality where the model is internally caching processed relational data, and the model becomes associated with new (or disassociated with) items. There is no method to inform the original model that it's cache is stale.

it would seem appropriate that all functions with the property 'alters_data' (originally intended for template use) would have pre/post signals.
(django/db/models/fields/, django/contrib/contenttypes/ add, create, remove, clear )

to give a trivial example:

class Attendee(models.Model):
	name = models.CharField(maxlength=255)

class Conference(models.Model):
	name = models.CharField(maxlength=255)
	attendees = models.ManyToManyField(Attendee, related_name='conferences')
	def num_attendees(self):
		if not hasattr(self,"attendee_count"):
		return self.attendee_count
	def stale_cache(self):
		if hasattr(self,"attendee_count"):

(generally, i'm looking to do more complex processes than take the len() of a queryset...)

a,b,c = Attendee(name="Alice"), Attendee(name="Bob"), Conference(name="PyCon")
for i in (a,b,c):


obviously calls to stale_cache() would fix the problem, but the application-level programmer shouldn't necessarily have to deal with cache maintenance. (DRY... someone's bound to forget to update cache somewhere), it would be better of there is a signal to do this on relational updates.

Enabling this should be fairly straightforward:

  • add appropriate signals to django.db.models.signals
  • call the dispatcher from appropriate functions in the relational model managers, etc

Attachments (2) (1.8 KB) - added by Andrew Gibson 9 years ago.
model tests for the m2m signals
patch.diff (7.4 KB) - added by Andrew Gibson 9 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 10 years ago by Marc Fargas

Triage Stage: UnreviewedAccepted

comment:2 Changed 10 years ago by remo

This is a big issue. Right now, it is impossible to perform tasks when relations are added or removed. Consider this use-case:

class Survey(models.Model):
    participants = models.ManyToManyField(User)

How would I send a message to an User when he is added to the Survey? Hooking into post_save of Survey does not work, as its unrelated to the 'participants' manager. #6095 would offer a way to achieve this (by hooking management code into an intermediary model). But using intermediary models will break the admin interface (eg. 'filter_horizontal'), and that's a huge consequence to adding some management code.

So signals seem to be the best solution to this problem. I suggest we name those signals 'pre/post_add_relation' and 'pre/post_remove_relation'. As I see it, there are two issues.

  1. When recieving such a signal, we'll need to know
    • the model instance with the m2m field (Survey)
    • the m2m field itself (Survey.participants, as there might be several m2m fields to User)
    • the related object (User instance)

We just have the sender and instance arguments to pass information.. maybe we can use a tuple to pass both related objects as instance and use the m2m field as sender?

  1. The ManyRelatedManager does alot of low-level stuff (ie. it uses custom queries to add objects, to remove objects and to clear itself). Sending appropriate signals would void many of these optimizations.

Thinking of it, we could refractor the m2m manager to use a generic intermediary model. This model would then send the stock pre/post_save signals. This would allow a much cleaner implementation of #6095, too.

comment:3 Changed 9 years ago by George Song

Cc: George Song added

comment:4 Changed 9 years ago by Andrew Gibson

Owner: changed from nobody to Andrew Gibson
Status: newassigned

Changed 9 years ago by Andrew Gibson

Attachment: added

model tests for the m2m signals

comment:5 Changed 9 years ago by Andrew Gibson

Has patch: set

comment:6 Changed 9 years ago by Chris Beaven

Needs documentation: set
Patch needs improvement: set

Docs would be good (/docs/ref/signals.txt)

Also, you can't use the new decorator format - Django has to be Python 2.3 compatible

Changed 9 years ago by Andrew Gibson

Attachment: patch.diff added

comment:7 in reply to:  6 Changed 9 years ago by Andrew Gibson

Replying to SmileyChris:

Docs would be good (/docs/ref/signals.txt)

Also, you can't use the new decorator format - Django has to be Python 2.3 compatible

removed new decorator format, added docs and put the whole patch into one file

comment:8 Changed 9 years ago by Christian Schilling

Cc: Christian Schilling added

comment:9 Changed 9 years ago by rvdrijst

Cc: rvdrijst added
Resolution: duplicate
Status: assignedclosed

Looks like a duplicate of #5390

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