Ticket #4339: file_field_delete_file_on_update.diff

File file_field_delete_file_on_update.diff, 1.2 KB (added by lakin.wecker@…, 16 years ago)

Patch which deletes a file when a new one is upload, but only if we're the only record which references that file.

  • 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        self.delete_file(original_object)
    784786        if new_data.get(upload_field_name, False):
    785787            func = getattr(new_object, 'save_%s_file' % self.name)
    786788            if rel:
Back to Top