Django: dynamically create / copy / delete DB

I know that I can use using() to select the database defined in settings.py for operations such as:

 User.objects.using('users_db').get(id=5).delete() 

The problem is that I need to dynamically create / copy / delete several different databases while the server is running (without restarting the server). So that I can change the database configuration in settings.py and select the active database in my code using using() .

Update: Please note that since we have access to the database settings through connections.databases after importing connections from django.db , the main problem is not how to determine the DB in setting.py . I am looking for django ORM commands that can create / copy / delete a database.

+1
source share
1 answer

There are two ways to do this.

  • You can use postgresql and use the json field to store information. In this case, you can use all the functions of django, and this is the recommended way to do what you are trying to achieve. Source - Jsonfield Django
  • You can use mongodb and create dynamic databases. But you cannot use Django ORM with this. (There is a django-mongodb-engine - Django-mongodb-engine, but it will not be useful for creating and deleting dynamic databases). Therefore, you should use Pymongo and write wrapper functions around it to create dynamic databases.
0
source

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


All Articles