forked from orbit-oss/flask
Update appfactories.rst, make extension related section clearer
This commit is contained in:
parent
5d20501604
commit
fc85bf42ae
1 changed files with 28 additions and 10 deletions
|
|
@ -54,20 +54,38 @@ get access to the application with the config? Use
|
||||||
|
|
||||||
Here we look up the name of a template in the config.
|
Here we look up the name of a template in the config.
|
||||||
|
|
||||||
Extension objects are not initially bound to an application. Using
|
Factories & Extensions
|
||||||
``db.init_app``, the app gets configured for the extension. No
|
----------------------
|
||||||
application-specific state is stored on the extension object, so one extension
|
|
||||||
object can be used for multiple apps. For more information about the design of
|
|
||||||
extensions refer to :doc:`/extensiondev`.
|
|
||||||
|
|
||||||
Your `model.py` might look like this when using `Flask-SQLAlchemy
|
It's preferable to create your extensions and app factories so that the
|
||||||
<http://pythonhosted.org/Flask-SQLAlchemy/>`_::
|
extension object does not initially get bound to the application.
|
||||||
|
|
||||||
|
Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_,
|
||||||
|
as an example, you should **not** do::
|
||||||
|
|
||||||
|
def create_app(config_filename):
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config.from_pyfile(config_filename)
|
||||||
|
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
But, rather, in model.py (or equivalent)::
|
||||||
|
|
||||||
from flask.ext.sqlalchemy import SQLAlchemy
|
|
||||||
# no app object passed! Instead we use use db.init_app in the factory.
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
|
||||||
# create some models
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
Using this design pattern, no application-specific state is stored on the
|
||||||
|
extension object, so one extension object can be used for multiple apps.
|
||||||
|
For more information about the design of extensions refer to :doc:`/extensiondev`.
|
||||||
|
|
||||||
Using Applications
|
Using Applications
|
||||||
------------------
|
------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue