Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fdw-forks
django-database-files
Commits
9ce9e2bf
Commit
9ce9e2bf
authored
Feb 22, 2012
by
chrisspen
Browse files
Added data migration to auto-load existing files into the database.
Updated documentation to note differences from parent.
parent
84cdd2fc
Changes
6
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
9ce9e2bf
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
database_files/management/commands/database_files_load.py
View file @
9ce9e2bf
...
...
@@ -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
)):
...
...
database_files/migrations/0001_initial.py
View file @
9ce9e2bf
# 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'
},
...
...
database_files/migrations/0002_load_files.py
0 → 100644
View file @
9ce9e2bf
# 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'
]
run_tests.sh
View file @
9ce9e2bf
#!/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
setup.py
View file @
9ce9e2bf
#!/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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment