Skip to main content

Custom FILE_STORAGE for Django. Saves model's filefields in your database instead of your file system.

Project description

Django DB File Storage is a custom file storage system for Django. Use it to save your models’ FileFields in your database instead of your file system.

How to use

Installing

If you still haven’t done it, install django-db-file-storage in your environment by typing the following code on your shell:

pip install django-db-file-storage

Settings

On your project’s settings, add 'db_file_storage' to your INSTALLED_APPS tuple.

Still on your project’s settings, set DEFAULT_FILE_STORAGE like this:

DEFAULT_FILE_STORAGE = 'db_file_storage.storage.DatabaseFileStorage'

URLs

Add the following URL pattern to your project’s main urlpatterns (/urls.py):

url(r'^files/', include('db_file_storage.urls')),

Models

For each FileField you want to save, you will need a separated model to hold the file in the database. I will refer to this extra model as the FileModel. The FileModel must have at least these fields:

  • a TextField() which will hold the encoded contents of the file

  • a CharField(max_length=255) which will hold the file’s name

  • a CharField(max_length=50) which will hold the file’s MIME type

For example (in a models.py file, inside an app called ‘console’):

class ConsolePicture(models.Model):
    bytes = models.TextField()
    filename = models.CharField(max_length=255)
    mimetype = models.CharField(max_length=50)

And the class which will have the FileField:

class Console(models.Model):
    name = models.CharField(max_length=100)
    picture = models.ImageField(upload_to='console.ConsolePicture/bytes/filename/mimetype', blank=True, null=True)

In this example, the FileField is actually an ImageField. It’s upload_to argument must be a string in the following format:

  1. the FileModel’s app’s name

  2. a dot (.)

  3. the FileModel’s name

  4. a forward slash (/)

  5. the name of the FileModel’s field which will hold the encoded contents of the files

  6. a forward slash

  7. the name of the FileModel’s field which will hold the name of the files

  8. a forward slash

  9. the name of the FileModel’s field which will hold the MIME type of the files

Let’s check it again:

#   1   2       3      4  5  6    7   8    9
'console.ConsolePicture/bytes/filename/mimetype'

Don’t forget to create the necessary tables in your database, if you haven’t yet.

Form widget

At this point, your project already must be saving files in the database when you use Django’s ModelForms.

However, due to Django DB File Storage’s internal logic, Django’s default widget for file inputs won’t show the proper filename when downloading uploaded files. The download itself works perfectly, it’s just the widget that doesn’t show the correct name in it’s download link.

Django DB File Storage comes with a custom widget to solve this problem: DBClearableFileInput. You just need to use it when defining your form class:

from console.models import Console
from db_file_storage.form_widgets import DBClearableFileInput
from django import forms

class FormConsole(forms.ModelForm):
    class Meta:
        model = Console
        widgets = {
            'picture': DBClearableFileInput
        }

Code & Demo

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

django-db-file-storage-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file django-db-file-storage-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-db-file-storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3180c70eca713ff14592dd06e53b6be73a1de3de0076ae7c44c5110cb531bcb9
MD5 92d665a4de261724eea601e693744c8c
BLAKE2b-256 72bbd90a4218c8c98fe2b47ba4a7280d80d677604633fa67f7b2590b6eff8a3e

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 Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page