根据django中的请求动态设置数据库

编程入门 行业动态 更新时间:2024-10-24 12:29:47
本文介绍了根据django中的请求动态设置数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用python-django编写一个多租户应用程序。

我想根据每个请求设置数据库连接。我以为我可以写一个中间件我们设置要用于该特定数据库的数据库。

import d from django.db import connections class SetTenantDatabase(object): def process_request(self,request): pattern = repile(\\b(http:// | https:// | www。| | 8000 |:| //)\\W\\d +,re.I) words = request.get_host() db_name = [pattern。 sub(,words)] [0] .split('。')[0] connections.databases ['new-alias'] = {'default':{ 'ENGINE':'django.db.backends.postgresql_psycopg2','NAME':'store1','USER':'xxx','PASSWORD':'xxx' 'HOST ':'127.0.0.1',} } conn = connections ['new-alias'] return无 / pre>

但这不工作。我该怎么办这个方法错了或解决方案可行,最后是怎么样?

解决方案

这是答案,希望将来可以帮助某人:

import re import threading request_cfg = threading.local() class RouterMiddleware(object): def process_request(self,请求): pattern = repile(\\b(http:// | https:// | www。| | 8000 |:| //)\\W\ \d +,re.I) words = request.get_host() db_name = [pattern.sub(,words)] [0] .split('。')[0] request_cfg.cfg = db_name return None def process_response(self,request,response): if hasattr(requ est_cfg,'cfg'): del request_cfg.cfg 返回响应 class DatabaseRouter(object): def _default_db(self): if hasattr(request_cfg,'cfg'): return request_cfg.cfg else: return'default' def db_for_read(self,model, **提示) return self._default_db() def db_for_write(self,model,** hints): return self._default_db()

谢谢

I am writing a multi-tenant application with python-django.

I want to set database connection based on each request.I thought i could write a middleware where we set the database to be used for that particular database.

import re from django.db import connections class SetTenantDatabase(object): def process_request(self, request): pattern = repile("\\b(||www.||8000|:|//)\\W\\d+", re.I) words = request.get_host() db_name = [pattern.sub("", words)][0].split('.')[0] connections.databases['new-alias'] = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'store1', 'USER': 'xxx', 'PASSWORD': 'xxx', 'HOST': '127.0.0.1', } } conn = connections['new-alias'] return None

but this is not working.How should i do this.Is the approach wrong or is the solution feasible, and lastly How?

解决方案

this is the answer, hope it helps someone in future:

import re import threading request_cfg = threading.local() class RouterMiddleware(object): def process_request( self, request): pattern = repile("\\b(||www.||8000|:|//)\\W\\d+", re.I) words = request.get_host() db_name = [pattern.sub("", words)][0].split('.')[0] request_cfg.cfg = db_name return None def process_response( self, request, response ): if hasattr( request_cfg, 'cfg' ): del request_cfg.cfg return response class DatabaseRouter (object): def _default_db( self ): if hasattr( request_cfg, 'cfg' ): return request_cfg.cfg else: return 'default' def db_for_read( self, model, **hints ): return self._default_db() def db_for_write( self, model, **hints ): return self._default_db()

Thanks

更多推荐

根据django中的请求动态设置数据库

本文发布于:2023-10-16 00:33:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   动态   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!