Skip to content

CouchDB with Python

Today I’ve been investigating CouchDB a little better (only heard some rumors about it before). It’s actually a pretty nice technology which can, in some places, be pretty useful… I tend to compare it to caching serialized PHP associative arrays or Python dict’s in a Memcached server using some specific prefixes, except it’s not really memory-based (it’s persistent), you get a complete query interface (views), there’s dataset versioning support (!), etc. While writing this I start to wonder what similarities I ever saw between CouchDB and a Python pickled dict in Memcached…

Anyway, one use case I saw was site user profiles: profile data is most of the time not relational at all, so why store it in a relational database, which makes it sometimes rather hard to add extra profile information fields, unless you use some dirty ‘save serialized form’ trick, which renders your data unqueryable? Storing profile information (using eg. a user’s primary email address or login name as key for the user profile document) in CouchDB allows you to extend the profile “schema” easily: just add a field to your profile editting form, make sure it’s processed server-side an stored in the profile document, and add some extra code to your profile rendering template so the extra field get displayed too. No need to alter SQL tables at all!

As in my last site project I also have some sort of user profiles, I was thinking about using CouchDB for storage of these objects. As the site is written using Django, it would be nice to be able to define a standard Django model for the profile, which would be stored in CouchDB, not insome SQL server. This way you can still enjoy newforms goodness, among others.

So I started some new project, called django-couchdb, which should in time provide a model base class (similar to django.db.models.Model), corresponding managers to query the data, and so on. I don’t know (yet) whether all this is possible to achieve, anyway, I started by creating a very basic Python class which allows you to access a CouchDB server in a very Pythonic way: using dicts. A Server is a dict consisting of Databases, a Database is a dict of Documents. All this implemented thanks to the goodness of the DictMixin base class.

The client is not finished yet, at least 3 TODO items are on my list:

  • Error/Exception handling
  • Revision handling
  • View support

Currently there is no support for any of these. Views should be easy to add, error handling a little harder. I think revision handling is the hardest part, escpecially on figuring out how to provide this functionality in a Pythonic manner.

You can find the current code in this Git repository. Patches or external branches are very welcome!

By the way: the website I referred to before has been launched. It’s only of any use (well, maybe) for dutch-speaking users though. You can visit it here. Yes, the template will change.

Posted in Development.

Tagged with , , .

5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Milan Andric says

    Hi this looks really interesting, I was wondering if couchdb might help with searching and ranking documents based on keywords. I should read up on the couchdb documentation since I know nothing about it.

    I’m looking at a news site that was built using Sphinx Search, and several Drupal modules. Now I’m wondering how you can do something like this in Django/Python. I can surely use Sphinx regardless of what powers the views (php or python) but I’m noticing more talk about couchdb from the python community.

    Anyway, just wondering if you might have a take on this, thanks.

  2. Ernesto Revilla says

    I updated your to comply with version 0.8 and PEP8. Just drop me a mail if you are interested. Anyway, I’ll have a look if it’s possible to merge it with python-couchdb.


  3. David Horner says

    Hey just looking around at the landscape with python and couchdb. I see your project hasn’t been touched since 2007. Is it still live and well? What’s the story?

    Thanks, and great website!

  4. Nicolas says

    @David: it is not alive (no more personal interest, for now), feel free to fork though.

  5. Manuel Vázquez says

    Are you still working on django-couchdb? Can you put it on Github?

    Best regards,

Some HTML is OK

or, reply to this post via trackback.