Created
July 31, 2014 13:34
-
-
Save JuniorLima/69a79e175e70ff89823f to your computer and use it in GitHub Desktop.
Apagar imagem do diretório
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Photo(models.Model): | |
image = ImageField(...) # works with FileField also | |
def save(self, *args, **kwargs): | |
# delete old file when replacing by updating the file | |
try: | |
this = Photo.objects.get(id=self.id) | |
if this.image != self.image: | |
this.image.delete(save=False) | |
except: pass # when new photo then we do nothing, normal case | |
super(Photo, self).save(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment