简体   繁体   English

如何动态连接到数据库

[英]How to dynamically connect to a database

I want to connect Django to a database that the user will set in the main page. 我想将Django连接到用户将在主页上设置的数据库。

The user will have to precise the engine (with a combobox), the database, login, password etc in a form, and I want to proceed the connection with the submit. 用户将必须以表格的形式精确化引擎(带有组合框),数据库,登录名,密码等,而我想继续进行与提交的连接。

So far, I tried to set " DATABASES = {} " in settings.py , but it returns an error. 到目前为止,我尝试在settings.py设置“ DATABASES = {} ”,但是它返回错误。

Have you got any clue about how to do it? 您有关于如何做的任何线索吗?

You have to connect your app to a database. 您必须将您的应用程序连接到数据库。

There is no way that I know of to change the database connection dynamically. 据我所知,没有办法动态地更改数据库连接。 The only things I can think of are a bit convoluted: 我唯一能想到的就是有些混乱:

  1. If you want to change your back-end database: I would suggest having a separate setting file for the database (I will call it dbsettings.py ) that you will include in the settings file. 如果要更改后端数据库:建议您为数据库设置一个单独的设置文件(我将其称为dbsettings.py ),该文件将包含在设置文件中。 At the submit, you would lunch a script that will change the dbsettings.py file, and restart the django server (with a CLI call for example) 在提交时,您将吃午餐的脚本将更改dbsettings.py文件,然后重新启动django服务器(例如,使用CLI调用)

  2. If the database is user specific, configure and fire up another django instance, and have the two instances communicate (you could use celery for messaging or django commands) 如果数据库是特定于用户的,请配置并启动另一个django实例,并使两个实例进行通信(您可以将celery用于消息传递或django命令)

I (and other people) may come up with better solutions if we had a little more details 如果我们有更多细节,我(和其他人)可能会提出更好的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM