Deleting files associated with model - django

Posted by alexBrand on Stack Overflow See other posts from Stack Overflow or by alexBrand
Published on 2012-10-15T01:50:04Z Indexed on 2012/10/15 3:37 UTC
Read the original article Hit count: 110

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about django