Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error while following Tumblelog Application with Flask and MongoEngine

I am following tumbleblog application here

my __init__.py:

from flask import Flask
from flask.ext.mongoengine import MongoEngine

app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': "sencha_web_service", 'username': "<username>", "password": "<password>"}
app.config["SECRET_KEY"] = "KeepThisS3cr3t"

db = MongoEngine(app)

if __name__ == '__main__':
    app.run()

I get the error:

mongoengine.connection.ConnectionError: Cannot connect to database default :
False is not a read preference.

I tried passing in "alias"="default" in app.config["MONGODB_SETTINGS"] but still getting the same error.

like image 261
Nidhin Bose J. Avatar asked Apr 08 '15 14:04

Nidhin Bose J.


1 Answers

In your MONGODB_SETTINGS dictionary, the key for the database name should be 'db', not 'DB' (i.e. all lowercase).

The error you're getting is because the MongoEngine extension cannot find the 'db' entry in your configuration, and so uses 'default' as the database name.

Edit

Upon further inspection, it seems this is a bug somewhere in (Flask-)MongoEngine (or possible pymongo) where the default value of read_preference in mongoengine.connect is False instead of an actual read preference, and is not transformed to the actual default in pymongo

If you add

from pymongo import read_preferences

to your imports and

'read_preference': read_preferences.ReadPreference.PRIMARY

to your config dictionary, it should work (that's the default read_preference in pymongo)

like image 135
Samuel Littley Avatar answered Oct 20 '22 08:10

Samuel Littley