# Template - you can put this code on admin/base.html or on
# admin/change_form.html, as you want
# Your ModelAdmin
from django.contrib.admin.options import ModelAdmin
class MyModelAdmin(ModelAdmin):
def save_form(self, request, form, change):
"""Deletes the file from fields FileField/ImageField if
their values have changed"""
obj = form.instance
if obj:
for field in obj._meta.fields:
if not isinstance(field, FileField):
continue
path = getattr(obj, field.name, None)
if path and os.path.isfile(path.path):
if field.name in form.changed_data or form.data.get('clear_image_'+field.name, ''):
os.unlink(path.path)
setattr(obj, field.name, None)
return super(MyModelAdmin, self).save_form(request, form, change)