from django.contrib.admin.util import unquote from django.db import models as django_models import os class CustomModelAdmin(admin.ModelAdmin): def change_view(self, request, object_id, extra_context=None): #Original ImageField attr's obj = self.get_object(request, unquote(object_id)) for field in obj._meta.fields: if (isinstance(field, django_models.ImageField) or isinstance(field, django_models.FileField))\ and request.FILES.has_key(field.name): setattr(obj, 'old_%s'%(field.name), getattr(obj, field.name)) ModelForm = self.get_form(request, obj) if request.method == 'POST': form = ModelForm(request.POST, request.FILES, instance=obj) if form.is_valid(): for field in obj._meta.fields: #Overwriting ImageField attr's if (isinstance(field, django_models.ImageField) or isinstance(field, django_models.FileField))\ and request.FILES.has_key(field.name): path = getattr(obj, 'old_%s'%(field.name)) if path and os.path.isfile(path.path): os.unlink(path.path) return super(CustomModelAdmin, self).change_view(request, object_id, extra_context=extra_context)