I'm overriding Django's model delete method in order to delete orphan files in the disk for image fields, something like this:
class Image(models.Model):
img = models.ImageField(upload_to=get_image_path)
...
def delete(self, *args, **kwargs):
self.img.delete()
super(Image, self).delete(*args, **kwargs)
This works fine when I delete single objects from the admin, but when I select multiple objects and delete them, this doesn't seem to get called. I have been googling for a while but haven't hit the right keywords to get the answer for this, nor the official documentation seems to talk about this subject.
It does:
For that to work, you can override delete method on
QuerySet
, and then apply thatQuerySet
as manager:Delete method of queryset works directly on the database. It does not call
Model.delete()
methods. From the docs:If you want to override Django administration interface's default behavior, you can write a custom
delete
action:https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/
Another method is to override
post_delete
(orpre_delete
) signal instead ofdelete
method:https://docs.djangoproject.com/en/1.7/ref/signals/#django.db.models.signals.post_delete