Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Django-nonrel Picking up Momentum with MongoDB

DZone's Guide to

Django-nonrel Picking up Momentum with MongoDB

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

The Python-based web framework, Django, has a dedicated NoSQL community that's putting the framework ahead of the curve, now that more developers are looking for non-relational solutions for web app scalability.  Django-nonrel is a port of Django that supports non-relational databases.  They just added MongoDB as another type of backend available for Django-nonrel.

Django-nonrel allows developers to write non-relational code that is portable to multiple databases.  It also facilitates better productivity with the database's native API by generating an abstraction that functions as an indexing module.  It efficiently emulates JOINs and other features in Django's ORM.  

MongoDB is the second backend to be supported in Django-nonrel.  This means you can use the Django ORM directly with MongoDB and write code for Django that runs on MongoDB or Google App Engine, the other backend supported by Django-nonrel.  The code is also likely to work with Django's SQL backends. 

The process for installing a MongoDB backend to be used with Django's ORM isn't very tough.  Simple queries will work on a single model as expected, but the MongoDB limitations should be considered when formulating queries.  The Django admin interface is limited by MongoDB's query capabilities, which means you can't do JOINs for example.  A few field types are unsupported, including the ManyToManyField.  A query that spans relations won't work, so you can't edit users via the admin interface, but there's a workaround that is applied when you add "djangotoolbox" during installation.  Multi-table inheritance, aggregates, transactions, and QuerySet.select_related() are other advanced Django features not supported.

Here are some general rules to follow when writing code for Django-nonrel:

  • Some backends (e.g., MongoDB) use a string instead of an integer for AutoField. If you want to be on the safe side always write code and urlpatterns that would work with both strings and integers.
  • There is no integrity guarantee. When deleting a model instance the related objects will not be deleted. This had to be done because such a deletion process can take too much time. We might solve this in a later release.
  • JOINs don't work (yet). Queries can only be executed on one single model (filters can't span relationships like user__username=...).
  • You can't use transactions. If your particular DB supports a special kind of transaction (e.g., run_in_transaction on App Engine) you have to use the platform-specific functions.
  • Not all DBs provide strong consistency. If you want your code to be fully portable you should assume an eventual consistency model.

The Django-nonrel developers would like to add more backends such as Redis, SimpleDB, CouchDB, etc. with the help of more contributors.  Right now Django-nonrel is in an experimental stage, according to some, but official non-relational support is a distinct possibility in Django 1.3 or further down the road.  Alex Gaynor is currently adding non-relational support to Django's ORM for this year's Google Summer of Code.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}