How do I get Django Admin to delete files when I remove an object from the database/model? How do I get Django Admin to delete files when I remove an object from the database/model? django django

How do I get Django Admin to delete files when I remove an object from the database/model?


You can receive the pre_delete or post_delete signal (see @toto_tico's comment below) and call the delete() method on the FileField object, thus (in models.py):

class MyModel(models.Model):    file = models.FileField()    ...# Receive the pre_delete signal and delete the file associated with the model instance.from django.db.models.signals import pre_deletefrom django.dispatch.dispatcher import receiver@receiver(pre_delete, sender=MyModel)def mymodel_delete(sender, instance, **kwargs):    # Pass false so FileField doesn't save the model.    instance.file.delete(False)


Try django-cleanup

pip install django-cleanup

settings.py

INSTALLED_APPS = (    ...    'django_cleanup.apps.CleanupConfig',)


Django 1.5 solution: I use post_delete for various reasons that are internal to my app.

from django.db.models.signals import post_deletefrom django.dispatch import receiver@receiver(post_delete, sender=Photo)def photo_post_delete_handler(sender, **kwargs):    photo = kwargs['instance']    storage, path = photo.original_image.storage, photo.original_image.path    storage.delete(path)

I stuck this at the bottom of the models.py file.

the original_image field is the ImageField in my Photo model.