When a request is received, and a view is invoked, Flask-SQLAlchemy will set up a database session object inside an application context. The following is a fully functional Flask app that uses the schema described earlier in a View that can be queried from /users:
from flask import Flask, render_template app = Flask(__name__) @app.route('/users') def users(): users = db.session.query(User) return render_template("users.html", users=users) if __name__ == '__main__': db.init_app(app) db.create_all(app=app) app.run()
When the db.session.query() method is called, it performs a query on the database and turns every result from the User table into User objects that are passed to the users.html Jinja template for ...