gpt4 book ai didi

django - 通过 URL 参数进行数据库路由

转载 作者:行者123 更新时间:2023-12-03 21:03:31 25 4
gpt4 key购买 nike

我正在开发具有多个数据库的 Django 项目。在应用程序中,我需要根据用户的请求将数据库连接从 Development-Database 切换到 Test-DB 或 Production-DB。 (数据库架构已设置且不可更改!)

我也用这个旧指南试了一下运气 here
不起作用。在数据库路由器中,我无法访问 threading.locals。

我也尝试过设置自定义数据库路由器。通过 session 变量,我尝试设置连接字符串。要读取 dbRouter 中的用户 session ,我需要确切的 session key ,否则我必须循环抛出所有 session 。

object.using('DB_CONNECTION) 的方式是 Not Acceptable 解决方案......对于许多依赖项。我想为登录用户全局设置连接,而不为每个模型功能提供数据库连接......

请给我一些如何解决这个问题的意见。

我应该能够基于一个数据库路由器返回 dbConnection
session 值...

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:

更新 2:
这是使用 session 的尝试。

我通过 session 中的中间件存储数据库连接。
在路由器中,我想访问请求的 session 。我的期望是,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 的结果相同......一个空值:-(

最佳答案

使用 threading.local在响应请求时设置变量 ( doc )。自定义路由器是要走的路。

资料来源:

  • https://github.com/ambitioninc/django-dynamic-db-router
  • https://dzone.com/articles/django-switching-databases

  • 取自 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 的用户.

    关于django - 通过 URL 参数进行数据库路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56111681/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com