如何改变一个中间件设置? [英] How to change settings in a middleware?

查看:135
本文介绍了如何改变一个中间件设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换个环境变量 DJANGO_SETTINGS_MODULE (以及其他一些),然后让所有相关的模块,如 django.conf django.db 等重载以反映新的设置模块的信息。新设置的模块都会有不同的数据库。我将在中间件这样做。
我能够通过重新加载几个模块与沿django.conf 来实现这一点, django.db 。所有新的SQL语句中对新的数据库解雇。
但是,这似乎是这样的hackish。

I'd like to change the environment variable DJANGO_SETTINGS_MODULE (along with a few others) and then have ALL relevant modules like django.conf, django.db etc reloaded to reflect the information from the new settings module. The new settings module will have different database. I will be doing this in a middleware. I was able to achieve this by reloading a few modules along with django.conf and django.db. All new SQL statements were fired against the new DB. But this appears to be so hackish.

我想这样做的主要原因是有相同的Apache子进程服务,而无需重新创建一个新的Apache子进程,重新加载整个事情不同的Django的应用程序的请求(不同的设置,而不是不同的应用程序)。

The main reason for me wanting to do this is to have the same apache child process serve requests for different django applications (different settings and not different apps) without having to recreate a new apache child process which reloads the whole thing.

有没有什么实现我想要做一个干净的方式?

Is there a clean way of achieving what I want to do?

谢谢,

UPDATE(19九月2014):我已经接受了丹尼尔·罗斯曼的答案,这似乎是在问这个问题的背景下的现实。由他提出的路由器的方法是什么,我探讨,但由于Django的事务类不使用路由器不能使用。路由器我presume存在不同的原因。该应用程序code碱基我的工作,这是pretty大,先后为默认或特定数据库别名吨transaction.commit_manually。我试图让这个以支持多个客户端数据库,无需更改应用程序code。
不过,我还是设法解决的主要问题这是支持多个客户端DB和其他设置。我不试图改变动态的设置也没有我用的路由器。我不是有所有客户端DB信息的单一settings.py。我猴子修补的连接处理程序返回基于中间件设置某些ENV变量(由code使用或其他特定的别名)的默认的别名不同的数据库连接。到目前为止,这工作得很好。如果我遇到任何问题,或者如果其他人可以指出潜在问题的办法,我会发布更新。

UPDATE (19-Sept-2014): I have accepted Daniel Roseman's answer as that seems to be the reality in the context of the question asked. The Router approach suggested by him was something that I explored but couldn't use because django's transaction classes don't use the router. The router I presume exists for a different reason. The application code base I'm working on, which is pretty large, has tons of transaction.commit_manually for the default or a specific db alias. I was trying to get this to support multiple client databases without changing the application code. However, I did manage to solve the main problem which was to support multiple client DBs and other settings. I don't try to change the settings on the fly nor do I use the router. I instead have a single settings.py with all client DB information. I monkey patched the connection handler to return a different database connection for 'default' alias (or other specific alias used by the code) based on certain env variables set in the middleware. So far this has worked fine. I will post an update if I run into any issues or if someone else can point out a potential issue with the approach.

推荐答案

没有,有没有办法做到这一点,这是一件非常好的事情,因为它是一个坏主意。没有理由使用相同的Apache进程对不同的网站。相反,你应该有不同的虚拟主机为您的每个站点,并让阿帕奇管理它们。

No, there is no way to do this, and that's a very good thing as it is a bad idea. There is no reason to use the same Apache process for different sites: instead you should have different virtual hosts for each of your sites, and let Apache manage them.

这篇关于如何改变一个中间件设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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