Ale*_*hes 5 django django-middleware django-models django-database django-sessions
我正在开发具有多个数据库的 Django 项目。在应用程序中,我需要根据用户的请求将数据库连接从 Development-Database 切换到 Test-DB 或 Production-DB。(数据库架构已设置且不可更改!)
我也在这里用这个旧指南试了运气 不起作用。在数据库路由器中,我无法访问 threading.locals。
我也尝试过设置自定义数据库路由器。通过会话变量,我尝试设置连接字符串。要读取 dbRouter 中的用户会话,我需要确切的会话密钥,否则我必须循环抛出所有会话。
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']
Run Code Online (Sandbox Code Playgroud)
更新 1: 感谢 @victorT 的贡献。我只是用给定的例子尝试过。还是没有达到目标……
这是我尝试过的。可能你会看到一个配置错误。
Django Version: 2.1.4
Python Version: 3.6.3
Exception Value: (1146, "Table 'app.myModel' doesn't exist")
Run Code Online (Sandbox Code Playgroud)
.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()
Run Code Online (Sandbox Code Playgroud)
.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
Run Code Online (Sandbox Code Playgroud)
.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)
Run Code Online (Sandbox Code Playgroud)
这是输出:
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:
Run Code Online (Sandbox Code Playgroud)
Update2: 这是使用会话的尝试。
我通过会话中的中间件存储数据库连接。在路由器中,我想访问请求的会话。我的期望是,Django 处理这个并且知道请求者。但我必须在 Session 键中给出
s = SessionStore(session_key='???')
Run Code Online (Sandbox Code Playgroud)
我没有到达路由器...
.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()
Run Code Online (Sandbox Code Playgroud)
.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']
Run Code Online (Sandbox Code Playgroud)
这与 threading.local 的结果相同......一个空值:-(
至少我有时间测试threadlocals包,它适用于 Django 2.1 和 Python 3.6.3。
.app/中间件.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'])
...
Run Code Online (Sandbox Code Playgroud)
.app/路由器.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
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1183 次 |
| 最近记录: |