Is it possible to update Django connections on the fly?

Can I add a new connection to the Django database on the fly?

I have an application that uses several databases (django 1.2.1), and while working it allows you to create new databases. I need to use this new database immediately ( django.db.connections[db_alias] ). Is a server reboot possible? Using the reload module here and there?

Thank you for your time.

+4
source share
1 answer

Maybe ... but not recommended ... You can access the current connection handler ...

Use something like this:

 from django.db import connections if not alias in connections.databases: connections.databases[alias] = connections.databases['default'] # Copy 'default' connections.databases[alias]['NAME'] = alias 

Make sure that you are not trying to add a new alias to the database dictionary when there is ANY database activity in the current thread.

The problem you need to overcome is that this code will need to be hosted somewhere, as it will always be affected by the current thread before trying to access the database. For this, I use middleware.

+3
source

Source: https://habr.com/ru/post/1314444/


All Articles