Skip to main content

QueryGetter

ClassDescriptorSource

class AnyClass(sa.orm.DeclarativeBase):
query = QueryGetter()

val: sa.orm.Query[AnyClass] = AnyClass.query

The getter of the query property.

Use this descriptor like this:

class Base(sa.orm.DeclarativeBase):
query = QueryGetter()

class NewModel(Base): ...

It will mimic the behavior of flask_sqlalchemy. Using

NewModel.query.filter(...).all()

will be equivalent with

db.session.query(NewModel).filter(...).all()
warning

After setting the descriptor query, users can only call it in the Flask application context. Otherwise it will raise a RuntimeError.

Aliases

This class can be acquired by

import flask_sqlalchemy_compat as fsc


fsc.QueryGetter
fsc.utilities.QueryGetter