Routing and middleware for multiple databases in Django

老子叫甜甜 提交于 2020-01-04 06:09:46

问题


I have a problem correctly setting my middleware and routers to support multiple databases, one for each language (I decided to keep them separate).

I tried to use this solution, but for now I didn't get much use of it.

In my settings.py databases and middleware are defined as follows:

DATABASES = {
    'default': {},
    'ru': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db_ru.sqlite3',
    },
    'en': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db_en.sqlite3',
    }
}

DATABASE_ROUTERS = ['me.middleware.database_routing.DatabaseRouter']

MIDDLEWARE = [
    'me.middleware.database_routing.RouterMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

database_routing.py used in 'me.middleware.database_routing' fully corresponds to this middleware.

I get an error when starting the server: RouterMiddleware() takes no arguments. I believe there's some shortcoming concerning the middleware code. Also I cannot migrate to my databases, I get ValueError: Cannot assign "<ContentType: ContentType object (1)>": the current database router prevents this relation.

Maybe, there is some other solution?

来源:https://stackoverflow.com/questions/56046982/routing-and-middleware-for-multiple-databases-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!