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@…, 8 years ago)
  • db/models/fields/__init__.py

     
    769769            file_name = getattr(instance, 'get_%s_filename' % self.name)()
    770770            # If the file exists and no other object of this type references it,
    771771            # delete it from the filesystem.
     772            manager = instance.__class__._default_manager
    772773            if os.path.exists(file_name) and \
    773                 not instance.__class__._default_manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}):
     774                not manager.filter(**{'%s__exact' % self.name: getattr(instance, self.attname)}).exclude(pk=instance._get_pk_val()):
    774775                os.remove(file_name)
    775776
    776777    def get_manipulator_field_objs(self):
     
    781782
    782783    def save_file(self, new_data, new_object, original_object, change, rel, save=True):
    783784        upload_field_name = self.get_manipulator_field_names('')[0]
     785        if original_object:
     786            self.delete_file(original_object)
    784787        if new_data.get(upload_field_name, False):
    785788            func = getattr(new_object, 'save_%s_file' % self.name)
    786789            if rel:
Back to Top