I have the following code in one of my models
class PostImage(models.Model):
post = models.ForeignKey(Post, related_name="images")
# @@@@ figure out a way to have image folders per user...
image = models.ImageField(upload_to='images')
image_infowindow = models.ImageField(upload_to='images')
image_thumb = models.ImageField(upload_to='images')
image_web = models.ImageField(upload_to='images')
description = models.CharField(max_length=100)
order = models.IntegerField(null=True)
IMAGE_SIZES = {
'image_infowindow':(70,70),
'image_thumb':(100,100),
'image_web':(640,480),
}
def delete(self, *args, **kwargs):
# delete files..
self.image.delete(save=False)
self.image_thumb.delete(save=False)
self.image_web.delete(save=False)
self.image_infowindow.delete(save=False)
super(PostImage, self).delete(*args, **kwargs)
I am trying to delete the files when the delete() method is called on PostImage. However, the files are not being removed.
As you can see, I am overriding the delete() method, and deleting each ImageField. For some reason however, the files are not being removed.
You can delete a model instance with multiple methods.
One method is by calling
delete():In this case the
delete()is called, hence the files will be removed. However you can also delete objects by using querysets:The difference is that using the latter method, Django will delete the objects in bulk by using SQL
DELETEcommand, hence thedelete()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_deleteDjango signal as follows:Please note that if you use this method, you don’t have to overwrite the
delete()method anymore.More about this here and here