Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update appfactories.rst, make extension related section clearer #884

Merged
merged 1 commit into from
Feb 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions docs/patterns/appfactories.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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()

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)



# create some models 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
------------------ ------------------
Expand Down