Skip to main content

A storage system for Django that stores uploaded files in both the database and file system.Upstream: https://pypi-hypernode.com/project/django-binary-database-files/ .+Python 3.1x.

Project description

Django Binary Database Files +Python 3.1x

Upstream: https://pypi-hypernode.com/project/django-binary-database-files/ +Python 3.1x

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 2.2 - 4.0

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 line:

DEFAULT_FILE_STORAGE = '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:

./pep8.sh

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

sudo apt-get install python3-dev python3.6-dev

To run unittests across multiple Python versions, install:

sudo apt-get install python3.6-minimal python3.6-dev python3.7-minimal python3.7-dev

To run all tests:

export TESTNAME=; tox

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

export TESTNAME=; tox -e py36-django22

To run a specific test:

export TESTNAME=.test_adding_file; tox -e py36-django22

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

python setup.py sdist bdist_wheel
twine check dist/*
twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

File details

Details for the file django-binary-database-files-dimaqq-py31x-1.0.17.post1.tar.gz.

File metadata

File hashes

Hashes for django-binary-database-files-dimaqq-py31x-1.0.17.post1.tar.gz
Algorithm Hash digest
SHA256 baaacddf0b04a023a07d4486fec6420a599a2d3c3fed068b36161f796d969796
MD5 22da265471df1c3aab60da14d6204432
BLAKE2b-256 c6130b5a20f689fcca60904395b2d5993393f3b1913911e2eae1a355d0215605

See more details on using hashes here.

File details

Details for the file django_binary_database_files_dimaqq_py31x-1.0.17.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_binary_database_files_dimaqq_py31x-1.0.17.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 72c7b69c542f51faa7a4fe9e8897f80b30d05afa890eb09563fe8e617f02a0e7
MD5 faffb952c1f59970854bfadda2e1f11d
BLAKE2b-256 f63f9f867390d6e335135f729c18aff5cad466ed089bd93d72131c887bcd4cd7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page