最新的 django 和 mongo 引擎,您需要编辑 settings.py 才能安装吗?

latest django and mongo engine, do you need to edit settings.py to install it?

this tutorial中,它从来没有说要编辑settings.py,在以前的文档中,它支持django 1.5,你需要编辑settings.py。

那么您是否需要编辑该文件?作者跳过那部分是因为它有点明显吗?

实际上我在新文档中没有看到 settings.py 这个名字,网络上也没有太多 mongo db django 教程。这里的问题,如果有的话,已经过时了。如果这是一个幼稚的问题,我很抱歉。

如果你想使用 pymongo,据我所知,你无法从 settings.py 连接,所以我不得不问一下。

就我个人而言,我喜欢在设置中包含我所有的数据库配置,所以我在 settings.py 中有 mongo 数据库配置以及我的关系数据库配置:

MONGO_DBS = {
    'default': {
        'alias': 'default',         # the alias that Documents refer to
        'name': 'default',          # the name of the database to connect to
        'host': 'localhost',        # the host
        'port': 27017,              # the port
        'username': '',             # not implemented
        'password': '',             # not implemented
        'enabled': False,           # whether or not we connect to this database
    },
}

然后,我有一小段代码在 settings.py 中运行(提示一些抱怨)并连接到所有相关的 mongo 实例:

from mongoengine import connect
import sys

if not (len(sys.argv) > 1 and sys.argv[1] == 'test'):
    # Don't run this if we're running in unit tests. The test runner will spin
    # up the appropriate databases and spin them down appropriately.
    for db_name in MONGO_DBS:
        db_meta = MONGO_DBS[db_name]
        if db_meta['enabled'] and 'alias' in db_meta:
            connect(db_meta['name'], alias=db_meta['alias'], host=db_meta['host'], port=db_meta['port'],
                lazy_connect=db_meta.get('lazy', True))

显然,就未进行身份验证而言,此代码仍然有些不完整。但这对您来说应该是一个合理的起点。

我应该补充一点,我刚刚在 mongoengine 的 django 文档页面中找到了对 settings.py 的引用。目前,它位于 http://docs.mongoengine.org/en/latest/django.html.

最后,我要补充一点,此建议适用于 mongoengine 0.8.7(截至此答案的最新版本)。带有未来版本的 YMMV。