蓝图,Flask中的PyMongo

时间:2015-10-16 09:13:05

标签: python mongodb flask pymongo

在我的蓝图中拾取我的mongo对象的正确方法是什么?

以下是我的父母login.py

的方法
app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2


app = Flask(__name__)
app.register_blueprint(child2.child2)
app.register_blueprint(child.child)

child.py

from app import app
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child = Blueprint('child', __name__)

child2.py与child结构相同:

from app import app
from flask.ext.pymongo import PyMongo

mongo = PyMongo(app)
child2 = Blueprint('child2', __name__)

以下是错误消息:

raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

我在蓝图中尝试了以下内容

mongo = app.data.driver

但是这给了:

AttributeError: 'Flask' object has no attribute 'data'

一旦我的应用创建了连接,我应该如何在我的蓝图中提取它?

这是完整的痕迹

Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from child import child
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"
(xxx)xxx@linux:~/xxx$ python login.py 
Traceback (most recent call last):
  File "login.py", line 12, in <module>
    from courses import courses
  File "/home/xxx/xxx/child/child.py", line 13, in <module>
    mongo = PyMongo(app) #blueprint
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "/home/xxx/xxx/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 121, in init_app
    raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"

所以问题是如何在每个蓝图中构建连接字符串到db的结构。这是文件结构:

login.py
config.py
/child/child.py
/child2/child2.py

这是config.py

MONGO_DBNAME = 'xxx'

MONGO_URL = os.environ.get('MONGO_URL')
if not MONGO_URL:
    MONGO_URL = "mongodb://xxx:xxxx@xxxx.mongolab.com:55822/heroku_xxx";

MONGO_URI = MONGO_URL

我在答案中尝试了以下建议,但这不起作用。请参阅下面我的评论,即预期答案。

2 个答案:

答案 0 :(得分:7)

Emanuel Ey建议在蓝图中执行导入的方法之一,结果证明它导致循环导入。经过多次播放后,事实证明唯一的方法(我能找到)是创建一个名为database.py的单独文件连接到数据库然后我可以通过蓝图导入此连接,如下所示:

child.py

from database import mongo
courses = Blueprint('courses', __name__)

和我的database.py

from flask.ext.pymongo import PyMongo
mongo = PyMongo() 

和app,login.py但必须初始化数据库

from database import mongo
app = Flask(__name__)
app.config.from_object('config')
mongo.init_app(app) # initialize here!

from child import child 
from child import2 child2

app.register_blueprint(child.child)
app.register_blueprint(child2.child2)

答案 1 :(得分:1)

您正在初始化PyMongo驱动程序两次,一次是child.py,另一次是child2.py

尝试在设置app对象的文件中初始化PyMongo连接,然后将其导入子项:

login.py:

app.config.from_object('config')
from flask.ext.pymongo import PyMongo
from child import child
from child2 import child2


app = Flask(__name__)
mongo = PyMongo(app)

# Register blueprints
def register_blueprints(app):
    # Prevents circular imports
    app.register_blueprint(child2.child2)
    app.register_blueprint(child.child)

register_blueprints(app)

在child.py中

from app import app, mongo

child = Blueprint('child', __name__)

child2.py:

from app import app, mongo

child2 = Blueprint('child2', __name__)