I'm thinking about shifting my site's backend to Mongo from Postgres for performance reasons, but key parts of the site rely on the GeoDjango models to calculate distances between objects in the real world (and so on).
Would it be feasible to have most of the site running on Mongo but those key areas using Postgres for storage? Is this painful and / or error-prone? Is there an all-Mongo solution I'm missing?
Any light you can shed on these matters for me would be much appreciated.
Since Django 1.2, you can define multiple datbase connections in your
settings.py
. Then you can use database routers to tell Django which database to go to, transparently for your application.Disclaimer: this is how I think it should work, I have never used MongoDB in Django, nor have I tested that my code actually works. :)
settings.py
Models
Then add custom Meta variables to your geo-tables, to override their database. Don't add this attribute to models that are supposed to go to the default database.
Database router
And write a database router to direct all models that have the
using
meta attribute set, to the appropriate connection:you can't have 'using' in the Meta list.
here is a working solution
add this to models.py:
create a router.py in your apps folder:
Content of router.py:
Reference router in your settings:
I would take a look at the Disqus talk from DjangoCan 2010 about their scaling architecture. They run quite possibly the largest Django website on top of Postgres. They present simple code snippets showing how to start both vertical and horizontal scaling using features built into Django.
My understanding is that they do use MongoDB for some of their analytics thought I don't think it's discussed in that talk.