No way to optimize batch deletion

60 views
Skip to first unread message

Antoine Catton

unread,
May 7, 2015, 7:38:42 PM5/7/15
to django-d...@googlegroups.com
Hello,

django-treebeard [1] has an optimized batch deletion for its models. [2]

The main reason is because post_delete signals are called for each
objects. And custom deletion in treebeard can be done in batch.

I was reading Django's code [3] and it looks like there's no way to hook
into _raw_delete() or DeleteQuery.delete_batch().

The only way is to hook into signals.post_delete.

This causes problems if we have something like this:

class A(treebeard.MP_Tree):
b = ForeignKey('B')

class B(models.Model):
a = IntegerField()

If we run:
B.objects.filter(expression).delete()
Because Treebeard is overriding A.objects.delete(), the custom delete()
will never be called on the tree, and the tree will be inconsistent.

One solution I can think of, would be to add a pre/post batch_delete
signal. But I'm open to any other solution.


[1] <http://code.tabo.pe/django-treebeard/src>
[2]
<http://code.tabo.pe/django-treebeard/src/04ca69b7daf6390bb2130305cf1325df486d6b7e/treebeard/mp_tree.py?at=default#cl-51>
[3]
<https://github.com/django/django/blob/3baebf52aa0850de3ebadcbe1eda7e6e9a2c6c44/django/db/models/deletion.py#L284>

--
Antoine Catton
Software Engineer at Fusionbox, Inc.
<https://www.fusionbox.com/>

Michael Manfre

unread,
May 7, 2015, 8:26:01 PM5/7/15
to django-d...@googlegroups.com
A pre/post batch_delete signal would be useful for the usage you describe. There would likely be confusion with its usage because unlike post_delete, the batch_delete signals would not be able to provide actual records. The best it could do would be to provide the queryset and the deletion count on the post. The topic of batch deletion signaling has come up before, but I don't remember the outcome (or whether it was on here or IRC). I'll try to find it and report back when I get to a more capable device.

Regards,
Michael Manfre

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/554BF717.50503%40fusionbox.com.
For more options, visit https://groups.google.com/d/optout.



--
GPG Fingerprint: 74DE D158 BAD0 EDF8
Reply all
Reply to author
Forward
0 new messages