I'm developing on a Django Project with several Databases. In an App I need to switch the database connectivity from a Development-Database to Test-DB or Production-DB, based on a request from the user. (DB Architecture is set and not changeable!)
I Tried my luck also with this old Guide here Does not work. Within the DB Router I have no access to the threading.locals.
I tried it also to set up a custom db router. Over a session variable, I tried to set the Connection string. To read the users Session in the dbRouter, I need the exact Session key, or I have to loop throw all Sessions.
The way over object.using('DB_CONNECTION) is a not acceptable solution… To many dependencies. I want to set a connection globally for a logged in User without giving the DB connection to each models function….
Please give me some imput how to solve this issue.
I should be able to return the dbConnection in a db Router based on a session value...
def db_for_read|write|*():
from django.contrib.sessions.backends.db import SessionStore
session = SessionStore(session_key='WhyINeedHereAKey_SessionKeyCouldBeUserId')
return session['app.dbConnection']
Update1: Thank you @victorT for your imputs. I just tried it with the given examples. Still not reached the target...
Here what i tried. Mabe you'll see a config error.
Django Version: 2.1.4
Python Version: 3.6.3
Exception Value: (1146, "Table 'app.myModel' doesn't exist")
.app/views/myView.py
from ..models import myModel
from ..thread_local import thread_local
class myView:
@thread_local(DB_FOR_READ_OVERRIDE='MY_DATABASE')
def get_queryset(self, *args, **kwargs):
return myModel.objects.get_queryset()
.app/myRouter.py
from .thread_local import get_thread_local
class myRouter:
def db_for_read(self, model, **hints):
myDbCon = get_thread_local('DB_FOR_READ_OVERRIDE', 'default')
print('Returning myDbCon:', myDbCon)
return myDbCon
.app/thread_local.py
import threading
from functools import wraps
threadlocal = threading.local()
class thread_local(object):
def __init__(self, **kwargs):
self.options = kwargs
def __enter__(self):
for attr, value in self.options.items():
print(attr, value)
setattr(threadlocal, attr, value)
def __exit__(self, exc_type, exc_value, traceback):
for attr in self.options.keys():
setattr(threadlocal, attr, None)
def __call__(self, test_func):
@wraps(test_func)
def inner(*args, **kwargs):
# the thread_local class is also a context manager
# which means it will call __enter__ and __exit__
with self:
return test_func(*args, **kwargs)
return inner
def get_thread_local(attr, default=None):
""" use this method from lower in the stack to get the value """
return getattr(threadlocal, attr, default)
This is the output:
Returning myDbCon: default
DEBUG (0.000) None; args=None
DEBUG (0.000) None; args=None
DEBUG (0.000) None; args=('2019-05-14 06:13:39.477467', '4agimu6ctbwgykvu31tmdvuzr5u94tgk')
DEBUG (0.001) None; args=(1,)
DB_FOR_READ_OVERRIDE MY_DATABASE # The local_router seems to get the given db Name,
Returning myDbCon: None # But disapears in the Router
DEBUG (0.000) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.001) None; args=()
Returning myDbCon: None
DEBUG (0.002) None; args=()
ERROR Internal Server Error: /app/env/list/ # It switches back to the default
Traceback (most recent call last):
File "/.../lib64/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/.../lib64/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 255, in execute
self.errorhandler(self, exc, value)
File "/.../lib64/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 252, in execute
res = self._query(query)
File "/.../lib64/python3.6/site-packages/MySQLdb/cursors.py", line 378, in _query
db.query(q)
File "/.../lib64/python3.6/site-packages/MySQLdb/connections.py", line 280, in query
_mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'app.myModel' doesn't exist")
The above exception was the direct cause of the following exception:
Update2: This is the try with the usage of sessions.
I store the db connection trough a middleware in the session. In the Router I want to access then the session that is requesting. My expectation would be, that Django handels this and knows the requester. But I have to give in the Session key as
s = SessionStore(session_key='???')
that i dont get to the router...
.middleware.py
from django.contrib.sessions.backends.file import SessionStore
class myMiddleware:
def process_view(self, request, view_func, view_args, view_kwargs):
s = SessionStore()
s['app.dbConnection'] = view_kwargs['MY_DATABASE']
s.create()
.myRouter.py
class myRouter:
def db_for_read(self, model, **hints):
from django.contrib.sessions.backends.file import SessionStore
s = SessionStore(session_key='???')
return s['app.dbConnection']
This results in the same as the threading.local... an emtpy value :-(
At least I had time to test the threadlocals package and it works with Django 2.1 and Python 3.6.3.
.app/middleware.py
from threadlocals.threadlocals import set_request_variable
try:
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object
class MyMiddleware(MiddlewareMixin):
def process_view(self, request, view_func, view_args, view_kwargs):
set_request_variable('dbConnection', view_kwargs['environment'])
...
.app/router.py
from threadlocals.threadlocals import get_request_variable
class MyRouter:
def db_for_read(self, model, **hints):
if model._meta.app_label == 'app':
return get_request_variable("dbConnection")
return None
...