简体   繁体   中英

How to dynamically connect to a database

I want to connect Django to a database that the user will set in the main page.

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.

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. 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)

  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)

I (and other people) may come up with better solutions if we had a little more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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