Skip to content

Instantly share code, notes, and snippets.

@Ma233
Created October 12, 2018 03:31
Show Gist options
  • Save Ma233/c54646755afcead0b409b6dcfe9472b3 to your computer and use it in GitHub Desktop.
Save Ma233/c54646755afcead0b409b6dcfe9472b3 to your computer and use it in GitHub Desktop.
sqlalchemy query
from sqlalchemy.orm import load_only
def model_query(M, *, select_=None, order_by_=None, conditions_=None, **kwargs):
q = M.query
if select_:
q = q.options(load_only(*select_))
if conditions_:
q = q.filter(*conditions_)
if order_by_:
q = q.order_by(*order_by_)
if kwargs:
q = q.filter_by(**kwargs)
return q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment