db_file_storage icon indicating copy to clipboard operation
db_file_storage copied to clipboard

Usage as a non-default media storage engine

Open jayvdb opened this issue 4 years ago • 0 comments

After quite a bit of mucking around, I was able to get this working without putting DEFAULT_FILE_STORAGE = 'db_file_storage.storage.DatabaseFileStorage' in my settings as is described in the README.

I wrote up my use case in a bit more detail at https://github.com/kimetrica/django-binary-database-files/issues/31 which I was also experimenting with.

I found how to get it working with the use of storage=DatabaseStorage(..) in their tests, and notice there isn't any similar tests or docs here which explain how to do that. See also https://github.com/kimetrica/django-binary-database-files/pull/32 about that. I had seen storage was available in the core Django field docs , but I tried quite a few incorrect incantations before realising how simple it was.

The models setup I have used is

from db_file_storage.storage import DatabaseFileStorage


class Filestore(models.Model):
    data = models.BinaryField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)


class Image(models.Model):
    name = models.CharField(max_length=100)
    image = models.ImageField(
        storage=DatabaseFileStorage(),
        upload_to='app.Filestore/data/filename/mimetype',
        blank=True, null=True)

I think this should be explained in the docs, and have tests to make sure it works correctly without relying on DEFAULT_FILE_STORAGE.

jayvdb avatar Aug 25 '20 05:08 jayvdb