Django - 运行时数据库切换 [英] Django - Runtime database switching

查看:366
本文介绍了Django - 运行时数据库切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作中,我们想要运行一个带有多个数据库的服务器。当您访问像 http://myapp.webpage.com http://other.webpage.com 。我们只想运行一个服务器实例,并且在HTTP请求的时刻切换数据库并返回相应的响应。



我们一直在寻找一个可维护的 Django友好的解决方案在我们的调查中,我们发现了可能的方法,但是我们还没有足够的信息。



选项1:Django中间件



django中间件每次服务器收到HTTP请求时都会运行。


  1. 在这里进行数据库切换可能是最佳选择,但使用 django数据库路由器据我所知,只允许更改模型或组或模型的数据库。


  2. 另一个选项是设置 django模型管理器中间件实例,并强制所有模型重新从对象属性从添加属性在自定义中间件。


  3. 我最后一个选项是在中间件接收的请求对象中创建一个新属性,返回数据库别名 settings.py ,每个模型查询使用使用方法




选项2:基于类的视图Mixin



创建一个mixin使用过去三个选项,但我必须在所有基于类的视图中设置mixin。如果一个程序员忘记设置mixin并且它来到一个生产服务器,数据可能是(或停止)在正确的数据库,我不想承担风险。



选项3:在运行时更改数据库设置



此选项适用于不推荐,风险太高。



更新:



如何运作?



middlewares.py

  import django.conf as conf 
import os.path


class SelectDB(object):

def process_request(self,request):
print request.META ['HTTP_REFERER']
file_database = open(booklog / database.txt,r)
database = file_database.read(10)
file_database.close()
如果database!='default':
conf.settings.DATABASES ['defau lt'] ['NAME'] =数据库

帮助我们解决的任何信息将不胜感激

解决方案

答案(对我有用)



已经在这里,在stackoverflow 中回答。我会喜欢这个功能在django。这很难找到使之成为可能的方式。



我认为这是很重要的评论伟大的工作, Wilduck 使用django插件 django-dynamic-db-router ,这是一个很好的插件,使得这种操作成为可能(有点)不同的方式。



非常感谢@JL Peyret和@ire_and_curses。



作为@ire_and_curses的答案。至少在这一刻,在我正在工作的项目中,这是我们需要的。在以前的项目中,我们需要一个类似的行为,每个实例都有一个服务器是可怕的,更新每个服务器,甚至自动化过程。


In my work we want to run a server with multiple databases. The databases switching should occur when you acces a url like http://myapp.webpage.com or http://other.webpage.com. We want to run only one server instance and at the moment of the HTTP request switch the database and return the corresponding response.

We've been looking for a mantainable and 'Django-friendly' solution. In our investigation we have found possible ways to do this, but we have not enough information about.

Option 1: Django middleware

The django middleware runs each time the server receive a HTTP request.

  1. Making a database switch here could be the best option but using django database routers as far as I know only allow to change the database for a model or group or models.

  2. Another option is to set a django model manager instance in the middleware and force all models to re-assign the objects attribute from an added attribute in the custom middleware.

  3. My last option is to create a new attribute in the request object received by the middleware that return the database alias from settings.py and in each model query use the using method.

Option 2: Class-based View Mixin

Create a mixin that use the past three options, but I the mixin must be set in ALL the Class-based views. If a programmer forget to set the mixin and it comes to a production server, the data could be (or stop being) in the right database, and I don't wanna take the risk.

Option 3: Changing the database settings in runtime

This option works but Is not recommended and is too risky.

UPDATE:

How this works?

middlewares.py

import django.conf as conf
import os.path


class SelectDB(object):

    def process_request(self, request):
        print request.META['HTTP_REFERER']
        file_database = open("booklog/database.txt", "r")
        database = file_database.read(10)
        file_database.close()
        if database != 'default':
            conf.settings.DATABASES['default']['NAME'] = database

Any information that help us to solve will be greatly appreciated.

解决方案

Answer (it worked for me)

The question was already answered here, in stackoverflow. I'd love this functionality were in django. It was a bit hard to find the way to make this possible.

I think that is important to comment the great work that Wilduck made with the django plugin django-dynamic-db-router, it's a great plugin that makes possible this operation in (a bit) different way.

Thanks a lot to @JL Peyret and @ire_and_curses.

And as an answer to @ire_and_curses. At least in this moment, in the project I'm working it's what we need. In previous projects we needed a similar behavior and made one server per instance was terrible to mantain and update each server, even automating the process.

这篇关于Django - 运行时数据库切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆