Code

Ticket #4339: file_field_delete_file_on_update.2.diff

File file_field_delete_file_on_update.2.diff, 1.2 KB (added by lakin.wecker@…, 6 years ago)
Line 
1Index: db/models/fields/__init__.py
2===================================================================
3--- db/models/fields/__init__.py        (revision 6903)
4+++ db/models/fields/__init__.py        (working copy)
5@@ -769,8 +769,9 @@
6             file_name = getattr(instance, 'get_%s_filename' % self.name)()
7             # If the file exists and no other object of this type references it,
8             # delete it from the filesystem.
9+            manager = instance.__class__._default_manager
10             if os.path.exists(file_name) and \
11-                not instance.__class__._default_manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}):
12+                not manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}).exclude(pk=instance._get_pk_val()):
13                 os.remove(file_name)
14 
15     def get_manipulator_field_objs(self):
16@@ -781,6 +782,8 @@
17 
18     def save_file(self, new_data, new_object, original_object, change, rel, save=True):
19         upload_field_name = self.get_manipulator_field_names('')[0]
20+        if original_object:
21+            self.delete_file(original_object)
22         if new_data.get(upload_field_name, False):
23             func = getattr(new_object, 'save_%s_file' % self.name)
24             if rel: