Skip to content

Instantly share code, notes, and snippets.

@rririanto
Last active March 28, 2022 16:09
Show Gist options
  • Save rririanto/698ae4570715719dc3df98501af95128 to your computer and use it in GitHub Desktop.
Save rririanto/698ae4570715719dc3df98501af95128 to your computer and use it in GitHub Desktop.
Django Delete File when remove an Object & Delete Old File when updating an Object
'''
@jimmyromanticde
Note:
python 2.x Only
Django >= 1.4
Problem:
1. django cannot remove files when deleting an objects in models/database
2. django cannot update/replace/delete files when updating an objects in models/database
more about this solution https://docs.djangoproject.com/es/1.9/ref/signals/
'''
#put in your models header/top
from django.db.models.signals import pre_delete, pre_save
from django.dispatch.dispatcher import receiver
from django.core.files.storage import default_storage
#sample models. so it will be clear for you to understand
#i just put some field that what i think important for this problem
class Resume(models.Model):
thumbs = models.ImageField(
upload_to='uploads/thumbnails/', blank=True, null=True)
........
class ResumeImage(models.Model):
resume = models.ForeignKey(Resume, related_name='resume_image')
photo = models.ImageField(
upload_to='uploads/resume/', blank=True, null=True)
.......
class ResumeFile(models.Model):
resume = models.ForeignKey(Resume, related_name='resume_file')
files = models.FileField(
upload_to='uploads/resumefile/', blank=True, null=True)
.......
#you can put this anything you want as long as the models objects has been define.
#answer of case/problem 1. django cannot remove files when deleting an objects in models/database
@receiver(pre_delete, sender=Resume)
def ResumeMainDel(sender, instance, **kwargs):
try:
default_storage.delete(instance.thumbs.path)
#when this resume are deleted. we need to remove all related files with this main Resume models
instance.resume_file.all().delete()
instance.resume_image.all().delete()
instance.clean()
except Exception,e:
#keep track the error if there is something wrong with the code.
#u can remove it later after the code has been clean/running well
print e
return False
return True
#before remove a resumefile object. remove the file first.
@receiver(pre_delete, sender=ResumeFile)
def ResumeFileDel(sender, instance, **kwargs):
try:
default_storage.delete(instance.files.path)
instance.clean()
except Exception,er:
#keep track the error if there is something wrong with the code.
#u can remove it later after the code has been clean/running well
print er
return False
return True
#before remove a resumeimage object. remove the file first.
@receiver(pre_delete, sender=ResumeImage)
def ResumeImageDel(sender, instance, **kwargs):
try:
default_storage.delete(instance.photo.path)
instance.clean()
except Exception,er:
#keep track the error if there is something wrong with the code.
#u can remove it later after the code has been clean/running well
print er
return False
return True
#answer of case/problem 2. django cannot update/replace/delete files when updating an objects in models/database
@receiver(pre_save, sender=Resume)
def ResumeTumbsChange(sender, instance, *args, **kwargs):
if not instance.pk:
return
try:
obj = Resume.objects.get(id=instance.id)
except Resume.DoesNotExist:
# object is not in db, nothing to worry about
return
# is the save due to an update of the actual image file?
if obj.thumbs and instance.thumbs and obj.thumbs != instance.thumbs:
# delete the old image file from the storage in favor of the new file
default_storage.delete(obj.thumbs.path)
#I hope it will be useful for you. comment me if im wrong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment