Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin & Model Deletion

I've got a bunch of classes that inherit from a common base class. This common base class does some cleaning up in its delete method.

class Base(models.Model):
    def delete(self):
        print "foo"

class Child(Base):
    def delete(self):
        print "bar"
        super(Child, self).delete()

When I call delete on the Child from the shell I get:

bar
foo

as expected

When I use the admin, the custom delete functions don't seem to get called. Am I missing something obvious?

Thanks,

Dom

p.s. This is obviously a simplified version of the code I'm using, apologies if it contains typos. Feel free to leave a comment and I'll fix it.

like image 656
Dominic Rodger Avatar asked Sep 03 '26 06:09

Dominic Rodger


1 Answers

According to this documentation bug report (Document that the admin bulk delete doesn't call Model.delete()), the admin's bulk delete does NOT call the model's delete function. If you're using bulk delete from the admin, this would explain your problem.

From the added documentation on this ticket, the advice is to write a custom bulk delete function that does call the model's delete function.

Further information on this behavior is here. The bulk delete action doesn't call the model's delete() method for the sake of efficiency.

like image 158
bchang Avatar answered Sep 04 '26 20:09

bchang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!