Untabify multi-db docs.

This commit is contained in:
James Bennett 2012-09-08 16:08:01 -04:00
parent 5d1f09f450
commit 408c10e541
1 changed files with 54 additions and 54 deletions

View File

@ -242,41 +242,41 @@ send queries for the ``auth`` app to ``auth_db``::
A router to control all database operations on models in the A router to control all database operations on models in the
auth application. auth application.
""" """
def db_for_read(self, model, **hints): def db_for_read(self, model, **hints):
""" """
Attempts to read auth models go to auth_db. Attempts to read auth models go to auth_db.
""" """
if model._meta.app_label == 'auth': if model._meta.app_label == 'auth':
return 'auth_db' return 'auth_db'
return None return None
def db_for_write(self, model, **hints): def db_for_write(self, model, **hints):
""" """
Attempts to write auth models go to auth_db. Attempts to write auth models go to auth_db.
""" """
if model._meta.app_label == 'auth': if model._meta.app_label == 'auth':
return 'auth_db' return 'auth_db'
return Non return Non
def allow_relation(self, obj1, obj2, **hints): def allow_relation(self, obj1, obj2, **hints):
""" """
Allow relations if a model in the auth app is involved. Allow relations if a model in the auth app is involved.
""" """
if obj1._meta.app_label == 'auth' or \ if obj1._meta.app_label == 'auth' or \
obj2._meta.app_label == 'auth': obj2._meta.app_label == 'auth':
return True return True
return None return None
def allow_syncdb(self, db, model): def allow_syncdb(self, db, model):
""" """
Make sure the auth app only appears in the 'auth_db' Make sure the auth app only appears in the 'auth_db'
database. database.
""" """
if db == 'auth_db': if db == 'auth_db':
return model._meta.app_label == 'auth' return model._meta.app_label == 'auth'
elif model._meta.app_label == 'auth': elif model._meta.app_label == 'auth':
return False return False
return None return None
And we also want a router that sends all other apps to the And we also want a router that sends all other apps to the
master/slave configuration, and randomly chooses a slave to read master/slave configuration, and randomly chooses a slave to read
@ -286,32 +286,32 @@ from::
class MasterSlaveRouter(object): class MasterSlaveRouter(object):
def db_for_read(self, model, **hints): def db_for_read(self, model, **hints):
""" """
Reads go to a randomly-chosen slave. Reads go to a randomly-chosen slave.
""" """
return random.choice(['slave1', 'slave2']) return random.choice(['slave1', 'slave2'])
def db_for_write(self, model, **hints): def db_for_write(self, model, **hints):
""" """
Writes always go to master. Writes always go to master.
""" """
return 'master' return 'master'
def allow_relation(self, obj1, obj2, **hints): def allow_relation(self, obj1, obj2, **hints):
""" """
Relations between objects are allowed if both objects are Relations between objects are allowed if both objects are
in the master/slave pool. in the master/slave pool.
""" """
db_list = ('master', 'slave1', 'slave2') db_list = ('master', 'slave1', 'slave2')
if obj1.state.db in db_list and obj2.state.db in db_list: if obj1.state.db in db_list and obj2.state.db in db_list:
return True return True
return None return None
def allow_syncdb(self, db, model): def allow_syncdb(self, db, model):
""" """
All non-auth models end up in this pool. All non-auth models end up in this pool.
""" """
return True return True
Finally, in the settings file, we add the following (substituting Finally, in the settings file, we add the following (substituting
``path.to.`` with the actual python path to the module(s) where the ``path.to.`` with the actual python path to the module(s) where the