Django:“显示数据库”功能?

发布于 2024-12-29 07:28:01 字数 401 浏览 1 评论 0原文

我有一个奇怪的遗留数据库用例:我有多个数据库,其中(1)完全相同的架构,但(2)非常不同的数据集。具有此架构的数据库(整个数据库)每周都会添加到总数据集中。

有没有办法 (1) 内省服务器来找出可用的数据库,如果是的话,有没有办法 (2) 通过 URL 而不是通过当前的每个模型解决方案(因为我的模型没有改变,只有关联的基础表)?

这种内省是否可以动态化,以便每次有人点击主页时我都可以向他们显示可用数据库的列表?

当然,通用解决方案更好,但目前仅使用 MySQL 的解决方案也是可以接受的。

(欧洲分子生物学实验室基因组库中的用例,每隔几个月作为一套 MySQL 数据库转储发布一次,每个物种一个数据库,核心模式包含大约 20 个表,可以很好地映射到大约 6 个应用程序。架构很稳定,多年来没有改变。)

I have a weird legacy database use-case: I have multiple databases, with (1) exactly the same schema, but (2) very different datasets. Databases, entire databases, with this schema, are being added to the total dataset every week.

Is there a way to (1) introspect the server to find out what databases are available, and if so, is there a way to (2) route to the correct database by URL, rather than by the current per-model solution (since my models don't change, only the associated underlying tables)?

Can this introspection be made dynamic, so every time someone hits the home page I can show them the list of available databases?

A generic solution is preferable, of course, but a MySQL-only solution is currently acceptable.

(The use case in the European Molecular Biology Lab's genome library, which is published every few months as a suite of MySQL database dumps, one database per species, with a core schema of about twenty tables which map nicely to six or so apps. The schema is stable and hasn't changed in years.)

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

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

发布评论

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

评论(1

薄暮涼年 2025-01-05 07:28:01

是的,您可以运行任何原始 SQL,并且 显示数据库也不例外。但更改可用数据库列表以及在它们之间切换将很困难。恐怕这需要对 django 的内部进行修改或猴子修补。

更新:等等!我研究了 django.db.connections 背后的代码,发现如果您只是在运行时扩展 settings.DATABASES ,那么您将能够使用 <代码中的 code>SomeModel.objects.using('some-new-database').all() 。还没有测试过,但相信这应该有效!

Yes, you are able to run any raw SQL, and show databases is not exception. But it will be hard to change list of available databases and to switch between them. I'm afraid this will require modification or monkey patching of django's internals.

Update: Wait! I've looked into the code behind the django.db.connections and found that if you just extend settings.DATABASES in runtime, then you'll be able to use SomeModel.objects.using('some-new-database').all() in the code. Have not tested, but belive this should work!

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