Integration with flask

Since Pony 0.7.4 we added support for comfortable using PonyORM with Flask. With pony.flask.Pony you can wrap your flask’s application’s request with db_session automatically right way.

from flask import Flask
from pony.flask import Pony

app = Flask(__name__)

With this code each of view function you will define will be wrapped with db_session so you should not care about them.


Also you can easily use Flask-Login extension.


from flask import Flask, render_template
from flask_login import LoginManager, UserMixin, login_required
from pony.flask import Pony
from pony.orm import Database, Required, Optional
from datetime import datetime

app = Flask(__name__)
    DEBUG = False,
    SECRET_KEY = 'secret_xxx',
    PONY = {
        'provider': 'sqlite',
        'filename': 'db.db3',
        'create_db': True

db = Database()

class User(db.Entity, UserMixin):
    login = Required(str, unique=True)
    password = Required(str)
    last_login = Optional(datetime)


login_manager = LoginManager(app)
login_manager.login_view = 'login'

def load_user(user_id):
    return db.User.get(id=user_id)

You can use LoginManager.current_user as User instance.

def friends():
    return render_template('friends.html', friends=current_user.friends)

You can run another example to check it

python -m pony.flask.example