http://flask.pocoo.org/docs/0.12/patterns/appfactories/flask 文档关于工厂模式里面提到了上面的这种套路。
It ’ s preferable to create your extensions and app factories so that the extension object does not initially get bound to the application.
Using Flask-SQLAlchemy, as an example, you should not do something along those lines:
def create_app(config_filename):
app = Flask(__name__)
app.config.from_pyfile(config_filename)
db = SQLAlchemy(app)
But, rather, in
model.py (or equivalent):
db = SQLAlchemy()
and in your
application.py (or equivalent):
def create_app(config_filename):
app = Flask(__name__)
app.config.from_pyfile(config_filename)
from yourapplication.model import db
db.init_app(app)