3

我该如何解决这个错误?

File "/usr/local/lib/python2.7/dist-packages/mongoengine/connection.py", line 95, in get_connection
    raise ConnectionError(msg)
mongoengine.connection.ConnectionError: You have not defined a default connection

模型.py:

from flask.ext.mongoengine import MongoEngine

db = MongoEngine()

class Currencies(db.Document):
    # meta = {"db_alias": "currencies"}
    name = db.StringField(max_length=40, min_length=3)
    def __unicode__(self):
        return self.name

app.py 中的 MongoEngine 配置:

from models import db

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {'DB':'testing'}
db.init_app(app)

视图.py:

from models import Currencies

print Currencies.objects.find()

Flask-Admin 正常工作。

4

2 回答 2

3

通过将值为“默认”的“别名”字段添加到配置来解决我的问题:

app.config['MONGODB_SETTINGS'] = {'db':'testing', 'alias':'default'}
于 2013-04-15T14:41:21.837 回答
1

您可以通过在配置文件中设置 DEFAULT_CONNECTION_NAME 来解决此问题,如下所示:

from mongoengine import connect

DEFAULT_CONNECTION_NAME = connect('blog')

于 2017-06-07T12:06:12.057 回答