Couchdb in Django

11
Leveraging CouchDB in Django Djangonauts May 3, 2011 Adam "Cezar" Jenkins [email protected]

description

Uploaded from Google Docs

Transcript of Couchdb in Django

Page 1: Couchdb in Django

Leveraging CouchDB in DjangoDjangonauts May 3, 2011

Adam "Cezar" Jenkins [email protected]

Page 2: Couchdb in Django

How we use CouchDB

• Not as a replacement for the Django ORM

• Only where it fits

• Leverage the Django ORMs strengths

• Maybe use it fully when it is better supported

Page 3: Couchdb in Django

Benefits

• Quick retrieval of large data sets.

• Document based records

o Prospective student applications went from 7 models to 1 document.

o Including resume, essay, and portfolio as attachments

o Documents in CouchDB are JSON

o Lists, Dictionaries, and nesting thereof.

Page 4: Couchdb in Django

• Provides convenient api to access CouchDB

• Django Extension

COUCHDB_DATABASES = (       ('appid.apply’, 'http://127.0.0.1:5984/apply’),

INSTALLED_APPS = (          ....          'couchdbkit.ext.django’,         .... )

Setup:

Page 5: Couchdb in Django

from couchdbkit.ext.django.schema import *

class Greeting(Document):     author = StringProperty()     content = StringProperty(required=True)     date = DateTimeProperty(default=datetime.utcnow)

Models:

Page 6: Couchdb in Django

apply/_designapply/_design/viewsapply/_design/views/by_useridapply/_design/views/by_userid/map.js

To create databases and sync CouchDB views, just run the usual `syncdb` command.

CouchDB Views:

function(doc) {   if(doc.doc_type == 'Application'){     emit(doc.user, doc);   } }

Couchdbkit allows you to keep your views in your source tree.

Page 7: Couchdb in Django

How we used it

• Used user.id to combine records

• May not work in other situations

from apply.models import Application

def get_application(user):     application = Application.view('apply/by_userid', key=user.id).first() 

    if not application:         raise Application.DoesNotExist, "User %s, no application found" % (user,)

    return application

Page 8: Couchdb in Django

Easy to integrate

import apply.forms as forms 

application = get_application(request.user)

form = forms.ApplicationForm(initial=application._doc) 

if form.is_valid():     application._doc.update(form.cleaned_data)     application.save()

• Django forms mostly use dictionaries

• CouchDBKit does have ModelForms, but were not used.

Page 9: Couchdb in Django

Speed

from apply.models import Application 

results = Application.view('apply/by_userid').all() 

4060.605 ms 

from couchdbkit.ext.django.loading import get_db 

db = get_db('apply') results = db.view('apply/by_userid').all() 

745.735 ms

• Sometimes using the couchdbkit models is just too slow

Page 10: Couchdb in Django

Attachments

form = forms.EssayForm(request.POST, request.FILES) if form.is_valid():     uploaded_file = request.FILES['essay']     application.put_attachment(uploaded_file, name = unicode(uploaded_file.name),                                                  content_length = uploaded_file.size)     application.save()

Uploaded Files

Downloading Attachments

def download_file(request, application, attachment_name):     attachment = application.fetch_attachment(attachment_name)     response = HttpResponse(attachment, content_type= \                                 application._attachments[attachment_name]['content_type'])     response['Content-Disposition'] = 'attachment; filename='+attachment_name     return response

Page 11: Couchdb in Django

CouchDB in Django

• Can be very useful for certain types of data

• Attachments are an amazing benefit

• With CouchDBkit integration is easy

• Django forms can be passed dictionaries

• Will be great when contrib apps can be loaded onto it.