Skip to content

Instantly share code, notes, and snippets.

@JuniorLima
Created July 31, 2014 13:34
Show Gist options
  • Save JuniorLima/69a79e175e70ff89823f to your computer and use it in GitHub Desktop.
Save JuniorLima/69a79e175e70ff89823f to your computer and use it in GitHub Desktop.
Apagar imagem do diretório
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