Commit 9ce9e2bf authored by chrisspen's avatar chrisspen
Browse files

Added data migration to auto-load existing files into the database.

Updated documentation to note differences from parent.
parent 84cdd2fc
django-database-files
=====================
django-database-files is a storage system for Django that stores uploaded files
django-database-files is a storage system for Django that stores uploaded files
in the database.
WARNING: It is generally a bad idea to serve static files from Django,
......@@ -16,25 +16,38 @@ Requires:
Installation
------------
$ python setup.py install
$ sudo python setup.py install
Or via pip with:
$ sudo pip install https://github.com/chrisspen/django-database-files/zipball/master
Usage
-----
In ``settings.py``, add ``database_files`` to your ``INSTALLED_APPS`` and add this line:
In ``settings.py``, add ``database_files`` to your ``INSTALLED_APPS`` and add
this line:
DEFAULT_FILE_STORAGE = 'database_files.storage.DatabaseStorage'
Although ``upload_to`` is a required argument on ``FileField``, it is not used for
storing files in the database. Just set it to a dummy value:
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.
upload = models.FileField(upload_to='not required')
If you're using South, the initial model migrations will scan through all
existing models for ``FileFields`` or ``ImageFields`` and will automatically
load them into the database.
All your ``FileField`` and ``ImageField`` files will now be stored in the
database.
If for any reason you want to re-run this bulk import task, 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
Test suite
----------
$ ./run_tests.sh
......@@ -3,7 +3,7 @@ import os
from django.conf import settings
from django.core.files.storage import default_storage
from django.core.management.base import BaseCommand, CommandError
from django.db.models import FileField, ImageField
from django.db.models import FileField, ImageField, get_models
from optparse import make_option
......@@ -18,7 +18,6 @@ class Command(BaseCommand):
settings.DEBUG = False
try:
broken = 0 # Number of db records referencing missing files.
from django.db.models import get_models
for model in get_models():
for field in model._meta.fields:
if not isinstance(field, (FileField, ImageField)):
......
# encoding: utf-8
import datetime
from django.core.management import call_command
from django.db import models
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
......@@ -17,13 +20,11 @@ class Migration(SchemaMigration):
))
db.send_create_signal('database_files', ['File'])
def backwards(self, orm):
# Deleting model 'File'
db.delete_table('database_files_file')
models = {
'database_files.file': {
'Meta': {'object_name': 'File'},
......
# encoding: utf-8
import datetime
from django.core.management import call_command
from django.db import models
from south.db import db
from south.v2 import DataMigration
class Migration(DataMigration):
def forwards(self, orm):
# Load any files referenced by existing models into the database.
call_command('database_files_load')
def backwards(self, orm):
import database_files
database_files.models.File.objects.all().delete()
models = {
'database_files.file': {
'Meta': {'object_name': 'File'},
'_content': ('django.db.models.fields.TextField', [], {'db_column': "'content'"}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}),
'size': ('django.db.models.fields.PositiveIntegerField', [], {})
}
}
complete_apps = ['database_files']
#!/bin/sh
PYTHONPATH=. DJANGO_SETTINGS_MODULE="database_files.tests.settings" django-admin.py test tests
#!/bin/bash
PYTHONPATH=. DJANGO_SETTINGS_MODULE="database_files.tests.settings" django-admin.py test tests
\ No newline at end of file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from distutils.core import setup
setup(
name='django-database-files',
version='0.1',
description='A storage system for Django that stores uploaded files in the database.',
author='Ben Firshman',
author_email='ben@firshman.co.uk',
url='http://github.com/bfirsh/django-database-files/',
description='A storage system for Django that stores uploaded files in both the database and file system.',
author='Chris Spencer',
author_email='chrisspen@gmail.com',
url='http://github.com/chrisspen/django-database-files',
packages=[
'database_files',
'database_files.management',
'database_files.management.commands',
'database_files.migrations',
],
classifiers=['Development Status :: 4 - Beta',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)
classifiers=[
'Development Status :: 4 - Beta',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
],
)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment