如何在Flask Blueprints中使用PyMongo? [英] How to use PyMongo with Flask Blueprints?
问题描述
在蓝图中拾取 mongo 对象的正确方法是什么?
What is the correct way of picking up mongo object inside Blueprints?
这是我的父母 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的结构相同:
child2.py
is the same structure as child:
from app import app
from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
child2 = Blueprint('child2', __name__)
这是我收到的错误消息:
This is the error message I get:
raise Exception('duplicate config_prefix "%s"' % config_prefix)
Exception: duplicate config_prefix "MONGO"
我已经在蓝图中尝试了以下方法
I've tried the following in the blueprint
mongo = app.data.driver
但是会引发错误.这是完整的追溯:
but that raises error. Here is full traceback:
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"
一旦我的应用创建了连接,我应该如何在我的蓝图中选择它?
Once my app has created the connection, how should I pick it up in my blueprints?
所以问题是,如何在每个蓝图中构造到db的连接字符串.这是文件结构:
So the question is how can one structure the connection strings to the db in each of the blueprints. Here is the file structure:
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
我已经尝试了以下建议中的建议,但这没有用.请参阅我在该预期答案下方的评论.
I've tried the suggestion below in answers, but this did not work. See my comments below that prospective answer.
推荐答案
按照Emanuel Ey的建议,在蓝图中执行导入方法的问题之一证明,这会导致循环导入.经过大量的测试,事实证明,唯一的方法(我可以找到)是创建一个单独的名为 database.py
的文件,该文件连接到数据库,然后可以按如下所示通过蓝图将此连接导入:
One of the issues with the approach of performing an import in the blueprint as was suggest by Emanuel Ey, turns out that it causes a circular import. After much playing, it turns out that the only way (I could find) was to create a separate file called database.py
that connects to the database and then I can import this connection to by blueprint as follows:
child.py
from database import mongo
courses = Blueprint('courses', __name__)
和我的 database.py
from flask.ext.pymongo import PyMongo
mongo = PyMongo()
和应用程序login.py,但必须初始化数据库
and the app, login.py but has to initialize the database
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)
这篇关于如何在Flask Blueprints中使用PyMongo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!