将Django +1.10与MongoDB连接 [英] Connecting Django +1.10 with MongoDB

查看:85
本文介绍了将Django +1.10与MongoDB连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个月中,是否有人在Django(+1.10)中替换了MongoDB的默认数据库引擎?我在Google中获得的所有信息都像6年前或4年前一样.

Does anybody replaced the default database engine in Django (+1.10) for MongoDB in the last months? All the info I get in Google is like 6 or 4 years ago.

最常见的结果包括 mongodb-engine ,它需要 django-nonrel (来自Django 1.5的非常老的分叉)或 mongoengine ,还有一个过时的库,它会提供许多错误,因此,在修补一个库时,会得到一个新的库.

The most commons results includes mongodb-engine which requires django-nonrel (a really old fork from Django 1.5), or mongoengine, also an outdated library that give many errors, so when you patch one, you get a new one.

我不知道是否有任何新"方式来实现这一目标.

I don't know if there is any "new" way to accomplish this.

推荐答案

在使用docker进行了几次构建和测试以尝试不同版本的组合后,我设法使用以下版本达到稳定且可正常运行的Django MongoDB应用程序:

With several builds and tests using docker to try different combinations of versions, I managed to reach to a stable and working Django MongoDB app using the following versions:

Django==1.10.0
pymongo==2.7.1
six==1.10.0
mongoengine==0.9.0

在安装所需的库之后,编辑您的settings.py以更新或添加以下内容

After installing the needed libraries, edit your settings.py to update or add the following

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.dummy'
    }
}
AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)
from mongoengine import *
    connect('database_name', host='host_ip',  port=27017)

更改数据库名称和主机ip.如果未指定IP,它将默认尝试连接到localhost.

Change the database name and host ip. It tries to connect to localhost by default if no ip is stated.

现在,运行您的应用程序应该已连接数据库并可以使用. 查看mongoengine 文档以获取有关如何使用它的信息.

Now, running your app should have the database connected and ready for use. Check mongoengine documentation for information on how to use it.

这篇关于将Django +1.10与MongoDB连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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