Django 在运行时修改 DATABASE_HOST

发布于 2024-08-10 20:11:58 字数 715 浏览 3 评论 0原文

我正在尝试在运行时在 2 个 mysql 服务器之间切换。我不需要始终保持两个连接处于活动状态。

这就是我正在做的事情

from django.conf import settings
from django.db import connection
from django.contrib.auth.models import User

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql1.com')
list1 = User.objects.all()

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql2.com')
list2 = User.objects.all()

我有以下settings.py:

DATABASE_HOST = '' # localhost
DATABASE_NAME = test
...

所有服务器上的数据库名称都是相同的,只有每个表的内容不同。

我应该得到 list1 != list2 因为两台服务器上的用户不同。

问题是我总是从settings.py(在本地主机上运行)中定义的默认数据库获取用户列表,而不是从 mysql 1 服务器然后从 mysql 2 服务器获取用户列表。

知道我在这里做错了什么吗?

洛朗

I am trying to switch between 2 mysql servers at runtime. I do not need to maintain both connections alive all the time.

This is what I am doing

from django.conf import settings
from django.db import connection
from django.contrib.auth.models import User

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql1.com')
list1 = User.objects.all()

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql2.com')
list2 = User.objects.all()

I have the following settings.py:

DATABASE_HOST = '' # localhost
DATABASE_NAME = test
...

The database name is the same on all servers and only the content of each tables differ.

I should get list1 != list2 as the users are different on both servers.

The issue is that I always get the list of users from the default database defined in settings.py (which is running on localhost) instead of the one from mysql 1 server and then from mysql 2 server.

Any idea what I am doing wrong here?

Laurent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-08-17 20:11:58

根据信息,我的猜测是您设置的 DATABASE_HOST 行中存在潜在错误(在上面的伪代码中)。读:“setattr(设置...”

除此之外,我不确定您如何配置数据库以根据您的条件进行切换,因为您没有解释这一点。如果您按模型进行操作,那么它可能值得考虑 Django 如何知道这一点,甚至使用外部连接(在渲染阶段之前手动加载数据库驱动程序并手动运行命令),并使用 main.

我会查询整个方法,但主要是因为我'我不确定您实际上如何区分这两个数据库,或者您能否提供更多有关如何执行此操作的信息?我认为您在上面的点 2 和 5 中提取的变量是不同的。我不需要这些值,我只是确保您没有使用旧的代码重复并且忘记编辑它(我们都在那里)

注意:如果可以的话,我会将其作为评论发布。 ,但我认为解决方案可能在于如何提取变量。最后,如果您处于“dev”/debug(离线状态),您可以尝试将数据库名称(只是服务器IP或其他内容)添加到输出中。 /非生产)模式,检查它是否确实到达第二个服务器。

My guess, from the information, would be a potential error in your set DATABASE_HOST lines (in yor pseudo code above). read: "setattr(settings..."

Other than that, I'm not sure how you've configured your database to switch based on your criteria, as you've not explained this. If you are doing it by model, it may be worth considering how Django knows this, or even using external connections (manually loading the database driver and running commands by hand prior to the render stage), and using the main.

I'd query the whole approach, but mostly because I'm not sure how you're actually differentiating the two databases, or why. Could you provide a bit more information on how you're doing this? I assume the variables you're pulling in dot-points 2 and 5 above are different. I don't need the values, I'm just making sure you've not used the old code duplication and forgotten to edit it (we've all been there).

Note: I'd post this as a comment if I could, but I think the solution may be in how you're pulling the variables. Finally, you could try adding the database name (just the server IP or whatever) to the output, if you're in 'dev'/debug (offline/non-production) mode, to check if it's actually making it to the second server.

帝王念 2024-08-17 20:11:58

作为参考,Django 文档明确指出您不应该这样做 - 在运行时更改设置

Django 社区中有很多关于同时支持多个连接/数据库的 ORM 的讨论。那里有很多很好的参考信息。查看此博客文章:对 Django 的简单多数据库支持 和此 Django wiki 页面多数据库支持

在博客文章中,Eric Florenzano 在他的 settings.py 文件中做了类似的事情:

DATABASES = dict(
    primary = dict(
        DATABASE_NAME=DATABASE_NAME,
        # ...
    ),
    secondary = dict(
        DATABASE_NAME='secondary.db',
        # ...
    ),
)

For reference, the Django documentation explicitly states you shouldn't do this -- Altering settings at runetime.

There is a lot of talk within the Django community about the ORM supporting multiple connections/databases at once. There's a lot of good reference info out there on it. Check out this blog post: Easy Multi-Database Support for Django and this Django wiki page Multiple Database Support.

In the blog post, Eric Florenzano does something like this in his settings.py file:

DATABASES = dict(
    primary = dict(
        DATABASE_NAME=DATABASE_NAME,
        # ...
    ),
    secondary = dict(
        DATABASE_NAME='secondary.db',
        # ...
    ),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文