如何选择数据库路由器?我正在使用子域,如果例如app1.domain.de
路由,我想更改数据库路由器。有人能帮帮我吗?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(SITE_ROOT, 'db') + '/default.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'app1': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(SITE_ROOT, 'db') + '/app1.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
'app2': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(SITE_ROOT, 'db') + '/app2.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
},
}
DATABASE_ROUTERS = ['app1.routers.DatabaseRouter', 'app2.routers.DatabaseRouter']
/app1/routers.py
class DatabaseRouter(object):
def db_for_read(self, model, **hints):
return "app1"
def db_for_write(self, model, **hints):
return "app1"
/app2/routers.py
class DatabaseRouter(object):
def db_for_read(self, model, **hints):
return "app2"
def db_for_write(self, model, **hints):
return "app2"