Running Python by interactive mode when using Flask factory pattern
Aug 28, 2017 · 1 min read
When we are using Flask factory pattern, we can divide configuration and implementation, also declare dependencies easily.
But I cannot use my app object and model classes directly in interactive mode when using factory pattern.
>>> from app.models import User, Board
>>> Board.query.search('foo')
...RuntimeError: application not registered on db instance and no applicationbound to current context
How can I resolve this RuntimeError? Exception message tells a solution. We should run that codes at context of application. Please see below code blocks.
app = create_app()with app.app_context():
result = Board.query.search('asd')
It didn’t show exception. First create flask application by create_app() and cover your code with app_context() when you are using interactive mode.
