I am trying to create a “modular application” in Flask using Blueprints.
When creating models, however, I encounter the problem of accessing the application to get the db object provided by Flask-SQLAlchemy. I would like to be able to use some drawings with multiple applications (similar to how Django applications can be used), so this is not a good solution. *
- You can make switcharoo and create Blueprint to create a
db instance that the application then imports along with the rest of the drawing. But then any other project that wants to create models should import from this project instead of the application.
My questions are as follows:
- Is there a way for Blueprints to define models without any knowledge of the application they will use later - and are there several drawings together? By this I mean the need to import the application module / package from your Blueprint.
- Am I wrong from the start? Are the drawings non-application independent and not distributed (la Django applications)?
- If not, which template should be used to create something like this? Jar extensions? If you just don’t do this - and perhaps centralize all à la Ruby on Rails models / schemes?
Edit: I already thought about it myself, and it could be more related to SQLAlchemy than to Flask, because when declaring models you need to have declarative_base() . And it must happen from somewhere, anyway!
Perhaps the best solution is to define a project diagram in one place and distribute it, as Ruby on Rails does. SQLAlchemy's declarative class definitions are really more like schema.rb than Django models.py. I suggest that this would also facilitate the use of migrations (from alembic or sqlalchemy-migrate ).
I was asked to give an example, so let's do something simple: let's say I have a plan that describes "flatpages" - simple, "static" content stored in a database. It uses a table with a simple name (for URLs), a title and a body. This is simple_pages/__init__.py :
from flask import Blueprint, render_template from .models import Page flat_pages = Blueprint('flat_pages', __name__, template_folder='templates') @flat_pages.route('/<page>') def show(page): page_object = Page.query.filter_by(name=page).first() return render_template('pages/{}.html'.format(page), page=page_object)
Then it would be nice to let this scheme define its own model (this is in simple_page/models.py ):
This question is related to:
- Flask-SQLAlchemy Import / Context
- What is your module layout folder for a Flask application?
And others, but all of the answers, seem to rely on importing the app db instance or the opposite. The Big App Like page of the wiki page also uses the import your app into your plan template.
* Since the official documentation shows how to create routes, views, templates and assets in Blueprint without worrying about which application it is “included”, I suggested that Blueprints should, in general, be reusable in applications, However, this modularity is not seems useful without independent models.
Since Blueprints can be connected to the application more than once, maybe this is the wrong approach to the model in the Drawings?