1

Before you tell me its a bad idea, I know its a bad idea. I'm not asking if its a good idea, I'm asking how to do it.


I have a requirement to store images from an ImageField from a reusable django app into a database, not necessarily the default database, just a database.

There is a python package that does this, but it requires altering the original model, which I can't do. Its not being hosted in the Google App Engine, so a custom solution for that doesn't work.

Ultimately, what I need is to be able to change the DEFAULT_FILE_STORAGE setting for a Django 1.9 app, and have all images and files from ImageField and FileField properties be stored into a database.

Is this possible?

Community
  • 1
  • 1

1 Answers1

1

You'll need to use a custom file storage class and assign it to DEFAULT_FILE_STORAGE in order to override the built-in ImageField and FileField behaviors, as described in the docs: https://docs.djangoproject.com/en/1.9/howto/custom-file-storage/

As it has it, there's another project besides the one you mention that's called https://github.com/bfirsh/django-database-files for this same purpose "django-database-files is a storage system for Django that stores uploaded files in the database.", which provides a custom class like the one you would need:

https://github.com/bfirsh/django-database-files/blob/master/database_files/storage.py

This custom file storage class targets Django 1.1, so it may be somewhat dated, but after a quick scan it does appear to implement the methods described in the updated docs 1. If it isn't a drop-in, it should still be helpful as a starting point to implement the class need for database storage.

  • Took about 15 minutes of hacking, but this answer still works! Thanks. –  Feb 23 '16 at 02:52