Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created March 27, 2011 22:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jacobian/889692 to your computer and use it in GitHub Desktop.
Save jacobian/889692 to your computer and use it in GitHub Desktop.
DeletingFileField
from django.db import models
from django.db.models import signals
class DeletingFileField(models.FileField):
"""
FileField subclass that deletes the refernced file when the model object
itself is deleted.
WARNING: Be careful using this class - it can cause data loss! This class
makes at attempt to see if the file's referenced elsewhere, but it can get
it wrong in any number of cases.
"""
def contribute_to_class(self, cls, name):
super(DeletingFileField, self).contribute_to_class(cls, name)
signals.post_delete.connect(self.delete_file, sender=cls)
def delete_file(self, instance, sender, **kwargs):
file = getattr(instance, self.attname)
# If no other object of this type references the file,
# and it's not the default value for future objects,
# delete it from the backend.
if file and file.name != self.default and \
not sender._default_manager.filter(**{self.name: file.name}):
file.delete(save=False)
elif file:
# Otherwise, just close the file, so it doesn't tie up resources.
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment