Package: django-binary-database-files
Django Binary Database FilesThis is a storage system for Django that stores uploaded
files in binary fields in the database. Files can be served from the database
(usually a bad idea), the file system, or a CDN.WARNING: It is generally a bad idea to serve static files from Django,
but there are some valid use cases. If your Django app is behind a caching
reverse proxy and you need to scale your application servers, it may be
simpler to store files in the database.Based upon django-database-files byKimetrica,rhunwicks,chrisspen,bfirshbut updated to work with Django 2.2-4.0, Python 3.6+ and to use a binary field for storage.Requires:Django 2.2 - 4.0InstallationSimply install via pip with:pip install django-binary-database-filesUsageInsettings.py, addbinary_database_filesto yourINSTALLED_APPSand add
this line:DEFAULT_FILE_STORAGE = 'binary_database_files.storage.DatabaseStorage'Note, theupload_toparameter is still used to synchronize the files stored
in the database with those on the file system, so new and existing fields
should still have a value that makes sense from your base media directory.If you are adding the package to an existing Django installation with pre-existing
files, run:python manage.py database_files_loadAdditionally, if you want to export all files in the database back to the file
system, run:python manage.py database_files_dumpNote, 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.
To delete all files in the database and file system not referenced by any model
fields, run:python manage.py database_files_cleanupSettingsDB_FILES_AUTO_EXPORT_DB_TO_FS=True|False(defaultTrue)If true, when a file is uploaded or read from the database, a copy will be
exported to your media directory corresponding to the FileField's upload_to
path, just as it would with the default Django file storage.If false, the file will only exist in the database.DATABASE_FILES_URL_METHOD='URL_METHOD_1'|'URL_METHOD_2'(default'URL_METHOD_1')Defines the method to use when rendering the web-accessible URL for a file.IfURL_METHOD_1, assumes all files have been exported to the filesystem and
uses the path corresponding to yoursettings.MEDIA_URL.IfURL_METHOD_2, uses the URL bound to thedatabase_fileview
to dynamically lookup and serve files from the filesystem or database.In this case, you will also need to updates yoururls.pyto include the view
that serves the files:urlpatterns = [
      # ... the rest of your URLconf goes here ...

      # Serve Database Files directly
      path(r"", include("binary_database_files.urls")),
  ]DATABASE_FILES_BASE_URLAllows theurlmethod of the storage backend to return an absolute URL if provided.DevelopmentCode should be linted with:./lint.shTests require the Python development headers to be installed, which you can install on Ubuntu with:sudo apt-get install python3.12-minimal python3.12-devTo run unittests across multiple Python versions, install:sudo apt-get install python3.10-minimal python3.10-dev python3.11-minimal python3.11-dev python3.12-minimal python3.12-devTo run alltests:export TESTNAME=; toxTo run tests for a specific environment (e.g. Python 3.12 with Django 5.0):export TESTNAME=; tox -e py312-django50To run a specific test:export TESTNAME=.test_adding_file; tox -e py312-django50To build and deploy a versioned package to PyPI, verifyall unittests are passing, then increase (and commit) the version number inbinary_database_files/__init__.pyand then run:python setup.py sdist bdist_wheel
twine check dist/*
twine upload dist/*