Flask Snippets

get_object_or_404

By Vitaliy Shishorin filed in Database

from sqlalchemy.orm import exc
from werkzeug.exceptions import abort

def get_object_or_404(model, *criterion):
    try:
        return model.query.filter(*criterion).one()
    except exc.NoResultFound, exc.MultipleResultsFound:
        abort(404)

Example:

board = get_object_or_404(Board, Board.slug == slug)

or

user = get_object_or_404(User, User.id == id)

This snippet by Vitaliy Shishorin can be used freely for anything you like. Consider it public domain.