From 56fdd60e7a54f19cd9fc10db475688a557ed7df7 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 4 Dec 2018 18:43:28 +0100 Subject: [PATCH 1/1] delete old file when attachment changes --- contact/forms.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contact/forms.py b/contact/forms.py index a9e3366..cc60c73 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import os + from django.contrib.sites.models import Site from django.core.exceptions import ValidationError from django.core.files.uploadedfile import UploadedFile @@ -141,7 +143,9 @@ class ContactForm(forms.Form): email_changed = True for name, value in self.cleaned_data.items(): if isinstance(value, UploadedFile): - Attachment.objects.filter(contact=contact, tag=name).delete() # delete files? + for attachment in Attachment.objects.filter(contact=contact, tag=name): + os.remove(attachment.file.path) + attachment.delete() attachment = Attachment(contact=contact, tag=name) attachment.file.save(value.name, value) attachment.save() -- 2.20.1