Skip to main content

Django Binary Database Files

Build Status

This 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 by Kimetrica, rhunwicks, chrisspen, bfirsh but updated to work with Django 2.2-4.0, Python 3.6+ and to use a binary field for storage.

Requires:

  • Django 4.2+

Installation

Simply install via pip with:

pip install django-binary-database-files

Usage

In settings.py, add binary_database_files to your INSTALLED_APPS and add this:

STORAGES = {
    "default": {
        "BACKEND": "binary_database_files.storage.DatabaseStorage"
    }
}

Note, the upload_to parameter 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_load

Additionally, if you want to export all files in the database back to the file system, run:

python manage.py database_files_dump

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. To delete all files in the database and file system not referenced by any model fields, run:

python manage.py database_files_cleanup

Settings

  • DB_FILES_AUTO_EXPORT_DB_TO_FS = True|False (default True)

    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.

    If URL_METHOD_1, assumes all files have been exported to the filesystem and uses the path corresponding to your settings.MEDIA_URL.

    If URL_METHOD_2, uses the URL bound to the database_file view to dynamically lookup and serve files from the filesystem or database.

    In this case, you will also need to updates your urls.py to 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_URL

    Allows the url method of the storage backend to return an absolute URL if provided.

Development

Code should be linted with:

./lint.sh

Tests require the Python development headers to be installed, which you can install on Ubuntu with:

sudo apt-get install python3.14-minimal python3.14-dev

To 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-dev

To run all tests:

export TESTNAME=; tox

To run tests for a specific environment (e.g. Python 3.12 with Django 5.0):

export TESTNAME=; tox -e py312-django50

To run a specific test:

export TESTNAME=.test_adding_file; tox -e py312-django50

To build and deploy a versioned package to PyPI, verify all unittests are passing, then increase the version number in binary_database_files/__init__.py and then run:

TAG=x.y.x
git add binary_database_files/__init__.py && git commit -m "Increase version to $TAG"
python setup.py sdist bdist_wheel
twine check dist/*
twine upload dist/*
git tag $TAG
git push --tags

Metadata

Release files for django-binary-database-files 1.0.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-binary-database-files 1.0.19
File Size Uploaded
django_binary_database_files-1.0.19.tar.gz 36.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-binary-database-files 1.0.19
File Interpreter ABI Platform
django_binary_database_files-1.0.19-py3-none-any.whl Python 3 none any Details

Total release size: 59.3 kB

Release files / django_binary_database_files-1.0.19.tar.gz

Download URL django_binary_database_files-1.0.19.tar.gz
Size 36.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2764ac9b9f4563853588386b6a3790573625e9d9edf35a7b34616fa50aa90c15
BLAKE2b-256 checksum
How to use checksums
2d1197fb2db5979193fecbb9786663da5804a6504ebccd35f7165cb36e16cc39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / django_binary_database_files-1.0.19-py3-none-any.whl

Download URL django_binary_database_files-1.0.19-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46be0b24557fd252401ac10e1be0c9a79ba02b4ee7ce79c3aa45be05bf97b1a1
BLAKE2b-256 checksum
How to use checksums
f2210581dabc8c0eec1805f830cbde780b0d32837f2a3783c12bb3aa078a0f78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

1.0.19 This release

2 release files

1.0.18

1 release file

1.0.17

2 release files

1.0.16

2 release files

1.0.15

1 release file

1.0.14

1 release file

1.0.13

1 release file

1.0.12

1 release file

1.0.11

1 release file

1.0.10

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page