Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Flask-SQLAlchemy in Blueprint models without reference to the app
    text
    copied!<p>I'm trying to create a "modular application" in Flask using Blueprints.</p> <p>When creating models, however, I'm running into the problem of having to reference the app in order to get the <code>db</code>-object provided by Flask-SQLAlchemy. I'd like to be able to use some blueprints with more than one app (similar to how Django apps can be used), so this is not a good solution.*</p> <ul> <li>It's possible to do a switcharoo, and have the Blueprint create the <code>db</code> instance, which the app then imports together with the rest of the blueprint. But then, any other blueprint wishing to create models need to import from <strong>that</strong> blueprint instead of the app.</li> </ul> <p>My questions are thus:</p> <ul> <li>Is there a way to let Blueprints define models without any awareness of the app they're being used in later -- and have several Blueprints come together? By this, I mean having to import the app module/package from your Blueprint.</li> <li>Am I wrong from the outset? Are Blueprints not meant to be independent of the app and be redistributable (à la Django apps)? <ul> <li>If not, then what pattern <em>should</em> you use to create something like that? Flask extensions? Should you simply not do it -- and maybe centralize all models/schemas à la Ruby on Rails?</li> </ul></li> </ul> <blockquote> <p><em>Edit</em>: I've been thinking about this myself now, and this might be more related to SQLAlchemy than Flask because you have to have the <code>declarative_base()</code> when declaring models. And <em>that's</em> got to come from somewhere, anyway!</p> <p>Perhaps the best solution is to have your project's schema defined in one place and spread it around, like Ruby on Rails does. Declarative SQLAlchemy class definitions are really more like schema.rb than Django's models.py. I imagine this would also make it easier to use migrations (from <a href="http://pypi.python.org/pypi/alembic/" rel="noreferrer">alembic</a> or <a href="http://code.google.com/p/sqlalchemy-migrate/" rel="noreferrer">sqlalchemy-migrate</a>).</p> </blockquote> <hr> <p>I was asked to provide an example, so let's do something simple: Say I have a blueprint describing "flatpages" -- simple, "static" content stored in the database. It uses a table with just shortname (for URLs), a title and a body. This is <code>simple_pages/__init__.py</code>:</p> <pre><code>from flask import Blueprint, render_template from .models import Page flat_pages = Blueprint('flat_pages', __name__, template_folder='templates') @flat_pages.route('/&lt;page&gt;') def show(page): page_object = Page.query.filter_by(name=page).first() return render_template('pages/{}.html'.format(page), page=page_object) </code></pre> <p>Then, it would be nice to let this blueprint define its own model (this in <code>simple_page/models.py</code>):</p> <pre><code># TODO Somehow get ahold of a `db` instance without referencing the app # I might get used in! class Page(db.Model): name = db.Column(db.String(255), primary_key=True) title = db.Column(db.String(255)) content = db.Column(db.String(255)) def __init__(self, name, title, content): self.name = name self.title = title self.content = content </code></pre> <hr> <p>This question is related to:</p> <ul> <li><a href="https://stackoverflow.com/questions/9692962/flask-sqlalchemy-import-context-issue">Flask-SQLAlchemy import/context issue</a></li> <li><a href="https://stackoverflow.com/questions/6089020/whats-your-folder-layout-for-a-flask-app-divided-in-modules?rq=1">What&#39;s your folder layout for a Flask app divided in modules?</a></li> </ul> <p>And various others, but all replies seem to rely on import the app's <code>db</code> instance, or doing the reverse. The <a href="https://github.com/mitsuhiko/flask/wiki/Large-app-how-to" rel="noreferrer">"Large app how to"</a> wiki page also uses the "import your app in your blueprint" pattern.</p> <p><sub>* Since the official documentation shows how to create routes, views, templates and assets in a Blueprint without caring about what app it's "in", I've assumed that Blueprints should, in general, be reusable across apps. However, this modularity doesn't seem <em>that</em> useful without also having independent models.</sub></p> <p><sub>Since Blueprints can be hooked into an app more than once, it might simply be the wrong approach to have models in Blueprints?</sub></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload