Using GridFS as Django file storage

News Site news RSS

Latest articles New articles RSS

Latest news from Twitter Follow Secretkeeper4 and on Twitter

Using GridFS as Django file storage

The method used here is also described on official documentation pages of mongoengine which I also used in this example.
First, mongoengine is needed as API for mongo database.
Somewhere in your file place following two lines:

import mongoengine
In you have to have something like this:
gridfs_storage = storage.GridFSStorage(base_url='/documents/')
# of course, put base_url value in as parameter.
def generate_file_name(filefield, name):
         # used to prevent Django in making up names of uploaded files
    return name
class Document(models.Model):
    created_on = models.DateTimeField(auto_now_add=True)
    doc = models.FileField(storage=gridfs_storage, upload_to=generate_file_name)
    def __unicode__(self):        
base_url is needed to let know Django admin interface from where it should download documents, and we have to prevent Django to store base_url as part of the file name into the GridFS.
Mongoengine's file storage appends '_<1..N>' suffix to file names if they already exist in database.
Last, but not least, place following configuration in Nginx configuration file:
location /documents/ {
               root_collection=fs field=filename type=string;
Nginx has to be compiled with nginx-gridfs module, found here: Also, you will have to modify Nginx Makefile in objs folder and remove gcc's option -Werror, since this module has some unused variables.
Remember, as of Django 1.3, file storage doesn't support deletion of documents, because of potential loss of data because of transactional unawareness. It is suggested for one to create a little cron job which should clean orphaned files.