The flask_sqlalchemy module flask_sqlalchemy not have to be initialized by the application right away - you can do this instead:
# apps.members.models from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Member(db.Model):
And then in your application settings you can call init_app :
# apps.application.py from flask import Flask from apps.members.models import db app = Flask(__name__)
In this way, cyclic imports can be avoided.
This template does not require placing all your models in one file. Just import the db variable into each of your model modules.
Example
# apps.members.models from apps.shared.models import db class Member(db.Model): # TODO: Implement this. pass
# apps.reporting.members from flask import render_template from apps.members.models import Member def report_on_members(): # TODO: Actually use arguments members = Member.filter(1==1).all() return render_template("report.html", members=members)
# apps.reporting.routes from flask import Blueprint from apps.reporting.members import report_on_members reporting = Blueprint("reporting", __name__) reporting.route("/member-report", methods=["GET","POST"])(report_on_members)
Note. this is a sketch of some of the features this gives you - obviously, you can do a little more to make development even easier (using the create_app template, autorecording drawings in specific folders, etc.)
Sean Vieira Mar 14 '12 at 2:15 2012-03-14 02:15
source share