Signal handling when deleting lots of objects?

20 views
Skip to first unread message

Stodge

unread,
Sep 1, 2016, 2:14:16 PM9/1/16
to Django users
I have two models, Volume and Group. The Volume model has a foreign key to Group.

When a user deletes a Volume the post_delete signal handler sends an HTTP DELETE request (/volume) to another server process. This works great. However, when the user deletes a Group, the cascading delete also deletes all volumes in that group. That means I get lots (I'm talking <100) of post_delete signals for the Volume model and therefore lots of HTTP requests.

Is there anyway to avoid this? Ideally, I'd like to send the HTTP DELETE  request (/volume) when a volume is deleted, but send a different HTTP DELETE request (/group) when the group is deleted and avoid sending any volume HTTP DELETE requests.

Thanks

Erik Cederstrand

unread,
Sep 1, 2016, 2:55:05 PM9/1/16
to Django Users
You could disconnect the post_delete signal for Volume temporarily, but that's a hack.

You probably have to abandon signals on the Volume model. I would attach the post_delete signals logic directly to the Volume.delete() method and add an option to disable signaling:

class Volume(models.Model):
[....]
def my_signal_logic(self):
do_whatever()

def delete(self, *args, **kwargs):
with_signal = kwargs.pop('signal', True)
if with_signal:
self.my_signal_logic()
super().delete(*args, **kwargs)


Then in the post_delete signal for Group, you delete the Volumes explicitly, telling delete() not to signal:

for v in instance.volumes.all():
v.delete(signal=False)

requests.delete('/group/%s' % instance.pk)


Erik

Stodge

unread,
Sep 1, 2016, 3:50:10 PM9/1/16
to Django users
Thanks Erik.

I forgot to say that the Group model has an "active" flag on it, so I'm seeing if I can use this.
Reply all
Reply to author
Forward
0 new messages