问题
What is the correct way of picking up my mongo object in my Blueprints?
Here is how I have my parent 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)
in my child.py
from app import app
from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
child = Blueprint('child', __name__)
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__)
Here is the error message:
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 gives:
AttributeError: 'Flask' object has no attribute 'data'
Once my app has created the connection, how should I pick it up in my blueprints?
Here is the full trace
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"
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
here is the 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.
回答1:
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__)
and my database.py
from flask.ext.pymongo import PyMongo
mongo = PyMongo()
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)
回答2:
You're are initializing the PyMongo driver twice, once in child.py
and a second time on child2.py
.
Try initializing the PyMongo connection in the file which sets up your app object and then import it in the children:
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)
in child.py
from app import app, mongo
child = Blueprint('child', __name__)
child2.py:
from app import app, mongo
child2 = Blueprint('child2', __name__)
来源:https://stackoverflow.com/questions/33166612/blueprints-pymongo-in-flask