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.