> Den 1. sep. 2016 kl. 20.14 skrev Stodge <stodge@gmail.com>:
>
> 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.
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
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/AD4D08EC-8C3E-49CC-BE1A-8358DC14F738%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment