0

I would like replace from django admin, one image for another and delete the old image. This is my models.py:

class Video(models.Model):

    name = models.CharField(verbose_name=u"Nom", max_length=64)

    machine = models.ManyToManyField(Maquina, verbose_name=u"Maquina/es", related_name='name_machine')
    reference = models.ManyToManyField(Referencia, verbose_name=u"Referencia/es", related_name='references')

    video = models.FileField(upload_to='videos', verbose_name=u"Video")
    photo = models.ImageField(upload_to='fotos_video', verbose_name=u"Imatge")

What function can I use for this? Thanks

awesoon
  • 32,469
  • 11
  • 74
  • 99
danivaro
  • 35
  • 5

1 Answers1

0

Django does not auto-remove files for you (think of it as a safety mechanism). If you want the photo removed, a good thing to do is use the pre_save signal with the Video model as a sender, check if the image has changed and if it did, os.unlink the old image.

Here is a close enough example for this.

Community
  • 1
  • 1
Andrei Avram
  • 949
  • 5
  • 7