我正在用几个数据库进行Django项目开发。在一个应用程序中,我需要根据用户的请求将数据库连接从开发数据库切换到测试数据库或生产数据库。 (数据库体系结构已设置且不可更改!)
我也通过这本旧指南here尝试过自己的运气 不起作用。在数据库路由器中,我无权访问threading.locals。
我也尝试过设置自定义数据库路由器。通过会话变量,我尝试设置连接字符串。要在dbRouter中读取用户Session,我需要确切的Session密钥,否则必须循环抛出所有Session。
克服object.using('DB_CONNECTION)的方式是不可接受的解决方案...对于许多依赖项。我想为登录的用户全局设置连接,而不给每个模型功能提供数据库连接…。
请给我一些解决方法。
我应该能够基于以下内容在数据库路由器中返回dbConnection: 会话值...
def db_for_read|write|*():
from django.contrib.sessions.backends.db import SessionStore
session = SessionStore(session_key='WhyINeedHereAKey_SessionKeyCouldBeUserId')
return session['app.dbConnection']
更新1: 谢谢@victorT的贡献。我只是通过给定的示例进行了尝试。 仍未达到目标...
这是我尝试过的。也许您会看到配置错误。
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)
这是输出:
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: 这是尝试使用会话的方法。
我在会话中通过中间件存储数据库连接。 在路由器中,我想访问正在请求的会话。我的期望是,Django处理此操作并知道请求者。但是我必须输入Session键为
s = SessionStore(session_key='???')
我不去路由器...
.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']
这与threading.local的结果相同...一个空值:-(
答案 0 :(得分:0)
在响应请求(doc)时,使用threading.local
设置变量。自定义路由器是必经之路。
来源:
取自django-dynamic-db-router
:
from dynamic_db_router import in_database
with in_database('non-default-db'):
result = run_complex_query()
请注意,该项目适用于Django 1.11 max,并且可能存在兼容性问题。尽管如此,两个来源中描述的路由器类和装饰器都非常简单。
根据您的情况,为每个用户设置变量。提醒一下,您将使用request.user
查找用户。
答案 1 :(得分:0)
至少我有时间测试threadlocals软件包,并且它与Django 2.1和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
...