0

I have a small problem with django, i'm beginner, and i would like to delete a file right before a new one is uploaded. I use this in my model.py

class Object(models.Model):
    photo1 = models.ImageField('main image', upload_to='%Y/%m/%d', max_length=200)

It works well for upload, but it keep the old image on hard drive. I can do a script to delete or move to a trash directory the file, but it's a bit problematic when using the django admin interface, because it won't update as i want. Is there a good way to delete the old photo1 variable object before storing the new one? Thanks for any help! (sorry for bad english)

maroufle
  • 1
  • 1

2 Answers2

0

Don't exist a automatically way to delete a file. You will need to do this manually using the package os or the image field for delete the file.

I recommend you to use signals to introduce this behavior. Docs

0

Thanks a lot, signals seems to do the job but i miss now the pure python code to deal with os (i'm new to python too :))

Edit: I finally just installed django-cleanup with pip and he do all that job alone, I just had to change ImageField for FileField because it doesnt have a file attribute. Cordially.

maroufle
  • 1
  • 1