Online Mind Mapping and Brainstorming

Create your own awesome maps

Online Mind Mapping and Brainstorming

Even on the go

with our free apps for iPhone, iPad and Android

Get Started

Already have an account? Log In

Multiple DB's, Ofer Malbin by Mind Map: Multiple DB
0.0 stars - reviews range from 0 to 5

Multiple DB's, Ofer Malbin

must have default entry

from 1.4, you'll have to use this syntax

How to use

need dictionary of database settings

syncdb --database=<db name>

automatic db routing

2 styles

automatic, define rules & then automatically decide which db to use


default routing scheme

sticky, Untitled

fallback to default db, if no definition

new class

Database Router, define 4 methods, where to read, example, Untitled, when returning None - the default db will be used, where to write, allow relation, example, Untitled, allow syncdb, def allow_syncdb(self, db, model):, whether to allow syncdb on specific model, using routers, define routers in settings, DATABASE_ROUTERS

example, MasterSlaveRouter, write on master, reads on slave db's, e.g., randomly choose a slave, allow relation, uses obj1.__state to check from which db the object arrived, see full example here:,

manual db selection

for example

Author.objects.using('default').all()'legcy'), if you did get with certain db, you can use save & it will stick to that db



Django & SaaS

sites app

1 db

relate models to sites

problem:, data of multiple customers on same db

multiple db's

1 per customer

example, sub-domain per customer, decide on db by sub-domain, full example,, even defines the DB's list in run-time!, middleware, saves the db in a dictionary (registry(, with the current thread as key, identify the sub-domain, looks up the db for this sub-domain, stores in the dictionary, provides a getter & setter to access the registry, db router, read & write, return the db from the middleware registry, relation & syncdb, return True


support in libraries is slowly arriving

transactions are problematic