1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'xxxx', 'USER': 'root', 'PASSWORD': '123456', 'HOST': '', 'PORT': '3306', 'OPTIONS': { 'init_command': 'SET storage_engine=MyISAM', }, }, 'analysis_db':{ 'ENGINE': 'django.db.backends.mysql', 'NAME': 'analysis_qq', 'HOST': '', 'USER': 'root', 'PASSWORD': '123456', 'PORT':'3306', 'OPTIONS':{ 'init_command': 'SET storage_engine=MyISAM', }, }, } class MasterSlaveRouter(object): def db_for_read(self, model, **hints): if model._meta.app_label == 'analysisqq': return 'analysis_db' else: return 'default' def db_for_write(self, model, **hints): if model._meta.app_label == 'analysisqq': return 'analysis_db' else: return 'default' def allow_relation(self, obj1, obj2, **hints): db_list = ('default','analysis_db',) if obj1._state.db in db_list and obj2._state.db in db_list: return True return None def allow_syncdb(self, db, model): if db == 'analysis_db': print " in analysis db" return True elif db == 'default': return False else: return None DATABASE_ROUTERS = ['db_router.MasterSlaveRouter']
|