I have the following model defined in my Django-App
def directory_path(instance, filename):
return 'attachments/{0}_{1}/{2}'.format(instance.date, instance.title, filename)
class Note(models.Model):
title = models.CharField(max_length=200)
date = models.DateField(default=datetime.date.today)
# File will be saved to MEDIA_ROOT/attachments
attachment = models.FileField(upload_to=directory_path)
def __str__(self):
return self.title
I successfully set up AWS S3/ Digitalocean Spaces to store the media file uploaded via the attachment field. However, when I delete an instance of the model, the file remains in my bucket. What do I have to do, in order to delete the file automatically in my s3 bucket, if the corresponding model intance is deleted?
CodePudding user response:
Ok, so the easiest way seems to be using the third-party-package django-cleanup
, see https://github.com/un1t/django-cleanup for details.
The only thing you need to do is install the app, add it at the bottom of your INSTALLED_APPS
in settings.py
and that's it.
When I delete a model instance, the file also gets automatically deleted in my Amazon S3/Digitalocean Spaces bucket.