﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
6157	Code to delete files when updating a filefield	aribao@…	nobody	"I'm using this code in my custom the save() methods. It looks for a filefield or imagefield, and delete the previous file when you insert a new one. Perhaps it could be a good idea to include it by defauly inside the save method of the object.

{{{
from django.db.models import get_model
import os
def save(self):
    mod = get_model( self._meta.app_label, self._meta.object_name )
    try:
        i = mod.objects.get(id=self.id)
    except:
        i = None
    else:
        for field in i._meta.fields:
            if field.get_internal_type() == 'FileField' or field.get_internal_type() == 'ImageField':
                if getattr(i,field.name) != getattr(self, field.name ):
                    path = getattr(i, 'get_%s_filename' % field.name)()
                    if os.path.isfile(path):
                        os.remove(path)
    super( mod, self ).save()
}}}"		closed	Database layer (models, ORM)	dev		wontfix	delete file update	aribao@…	Unreviewed	1	0	0	0	0	0
