user3541631
user3541631

Reputation: 4008

Django - File or Image Delete

The user has the possibility to add/delete image and documents.

I used GCBV UpdateView and a FormModel for this.

How can I offer the possibility to the user to delete the file/image ?

Upvotes: 0

Views: 1063

Answers (1)

Animesh Sharma
Animesh Sharma

Reputation: 3386

You will have to override the delete method of your model.

class YourModel(models.Model):
    file_field = models.FileField(upload_to='folder')

    def delete(self):
        os.remove(settings.MEDIA_ROOT+'folder/'+self.file_field.path)
        return super(YourModel,self).delete()

This will delete the entry from the model and also delete the file from your system.

Upvotes: 3

Related Questions