Reputation: 165
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?
Upvotes: 4
Views: 2203
Reputation: 41
if your database_routing.py contains the below section, it is stopping the relationships to create in Django.
So, try commenting on the commented lines and return only true. This worked for me!
def allow_relation(self, obj1, obj2, **hints):
"""
Do not allow relations involving the remote database
"""
# print("in allow relation")
# if obj1._meta.app_label == Organization or \
# obj2._meta.app_label == Organization:
# return True
# return None
return True
Upvotes: 1