Using GridFS as Django file storage

News Site news RSS

Latest articles New articles RSS

Latest news from Twitter Follow Secretkeeper4 and www.svesoftware.com 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 settings.py file place following two lines:

import mongoengine
mongoengine.connect('<my_database>')
 
In models.py you have to have something like this:
 
gridfs_storage = storage.GridFSStorage(base_url='/documents/')
# of course, put base_url value in settings.py 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):        
        return self.doc.name
 
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/ {
 gridfs
               cms
               root_collection=fs field=filename type=string;
               mongo 127.0.0.1:<port>;
}
 
Nginx has to be compiled with nginx-gridfs module, found here: https://github.com/mdirolf/nginx-gridfs. 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.