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