8π
β
You can delete a model instance with multiple methods.
One method is by calling delete()
:
PostImage.objects.get(...).delete()
In this case the delete()
is called, hence the files will be removed. However you can also delete objects by using querysets:
PostImage.objects.filter(...).delete()
The difference is that using the latter method, Django will delete the objects in bulk by using SQL DELETE
command, hence the delete()
method for each object is not called. So probably you are deleting objects using querysets, and therefore files are not being removed.
You can solve this by using post_delete
Django signal as follows:
@receiver(post_delete, sender=PostImage)
def post_delete_user(sender, instance, *args, **kwargs):
instance.image.delete(save=False)
instance.image_thumb.delete(save=False)
instance.image_web.delete(save=False)
instance.image_infowindow.delete(save=False)
Please note that if you use this method, you donβt have to overwrite the delete()
method anymore.
π€miki725
Source:stackexchange.com