Django:动态创建/复制/删除数据库 [英] Django: Creating/Copying/Removing DBs dynamically

查看:88
本文介绍了Django:动态创建/复制/删除数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用using()来选择settings.py中定义的DB来执行以下操作:

I know that I can employ using() to select a DB that is defined in settings.py for operations such as:

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

问题是我需要在服务器运行时动态创建/复制/删除几个不同的DB(而无需重新启动服务器).这样我就可以在settings.py中更改数据库配置,并通过使用using()在代码中选择活动数据库.

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

更新:请注意,由于从django.db导入connections后我们可以通过connections.databases访问数据库设置,所以主要问题不是如何在.我正在搜索可以创建/复制/删除数据库的django ORM命令.

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

推荐答案

两种处理方式.

  1. 您可以使用postgresql并使用json字段来存储您的信息. 在这种情况下,您可以使用django的所有功能,这是完成您要实现的目标的推荐方法. 来源- Jsonfield Django
  2. 您可以使用mongodb并创建动态数据库.但是您不能与此一起使用Django ORM. (有一个django-mongodb引擎- Django-mongodb-engine 但这对创建和删除动态数据库没有用.因此,您应该使用 Pymongo 并为其编写包装函数来创建动态数据库.
  1. You can use postgresql and use json field to store your informations. In this case you can use all django features and it is the recommended way to do what you trying to achieve. Source - Jsonfield Django
  2. You can use mongodb and create dynamic databases. But you cannot use Django ORM with this. ( There is an django-mongodb engine - Django-mongodb-engine but this won't be useful for creating and deleting dynamic databases). So you should use Pymongo and write wrapper functions around it create dynamic databases.

这篇关于Django:动态创建/复制/删除数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆