I am trying to design a simple api using flask, flask-restplus, and flask-pymongo but I'm facing one structural design, based on import and variables sharing, I cannot get access to the db in any way.
Here is my code in my main engine file:
app = Flask(__name__)
db = PyMongo(app)
api = Api(app)
from auth import namespace as ns1
api.add_namespace(registerNamespace.api)
if __name__ == '__main__':
api.run()
But at the same time, I would like to get access to the db instance in actual api implementation:
from engine import engine
api = Namespace('register', description="Registration")
db = engine.db
@api.route('/whatever')
Class Whatever():
def get(self):
db.doSomething();
return "Simple getter"
I get the following error.
ImportError: cannot import name engine
I've been trying to fix this in quite a long time, because I don't think it is that stupid of a structural decision but I probably am wrong. Which ways, either structural or import wise could help fixing this?
Thank you in advance!