AlexBrand
AlexBrand

Reputation: 12399

Deleting files associated with model - django

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.

Upvotes: 3

Views: 2436

Answers (1)

miki725
miki725

Reputation: 27861

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.

More about this here and here

Upvotes: 8

Related Questions