kimetrica / django-binary-database-files

A storage system for Django that stores uploaded files in the database.
Other
11 stars 18 forks source link

Automatically deleting referenced files #34

Open jayvdb opened 4 years ago

jayvdb commented 4 years ago

The README says

"Note that when a field referencing a file is cleared, the corresponding file in the database and on the file system will not be automatically deleted"

https://django-db-file-storage.readthedocs.io/en/master/#models contains an example of how to add automatic deletion for their solution:

from db_file_storage.model_utils import delete_file, delete_file_if_needed
from django.db import models

class Console(models.Model):
    name = models.CharField(max_length=100, unique=True)
    picture = models.ImageField(..., blank=True, null=True)

    def save(self, *args, **kwargs):
        delete_file_if_needed(self, 'picture')
        super(Console, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        super(Console, self).delete(*args, **kwargs)
        delete_file(self, 'picture')

Can something like that be added here?