Multiple databases (mongodb[mongoengine] and sql ) with django 1.8

最后都变了- 提交于 2019-12-02 07:11:54

问题


I am using mongoengine with Django and my project needs to connect to one instances of MongoDB while another with sql .How my databse section of setting.py should be like ?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'admin_db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
    },

}

from mongoengine import connect
connect(
db='pom',
username='admin',
password='root',
host='mongodb://admin:root@localhost'
)

回答1:


You could add multiple databases for your app in your settings.py like,

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'admin_db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
    },

    'your_desired_db_name' : {
        'ENGINE' : 'django_mongodb_engine',
        'NAME' : 'db_name'
}

For integration with mongodb, you may need to look up,

  • Django-nonrel
  • Django-MongoEngine

Also, you may need to look up Django documentation for multiple databases




回答2:


MongoEngine does not support all Django contrib modules directly. If your projects dont need them (unlikely) you can use mongoengine directly. Otherwise you can also try

  • djongo

Which seems to work fine with the latest Django version.



来源:https://stackoverflow.com/questions/44963440/multiple-databases-mongodbmongoengine-and-sql-with-django-1-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!