您尚未定义默认连接

时间:2013-04-10 12:11:44

标签: python flask mongoengine

我如何解决此错误?

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

model.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)

views.py:

from models import Currencies

print Currencies.objects.find()

Flask-Admin通常有效。

2 个答案:

答案 0 :(得分:1)

通过向config:

添加值'default'的'alias'字段来解决我的麻烦
app.config['MONGODB_SETTINGS'] = {'db':'testing', 'alias':'default'}

答案 1 :(得分:1)

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

from mongoengine import connect

DEFAULT_CONNECTION_NAME = connect('blog')