Chrome Extension

Using OR on filter query SQLAlchemy

You can apply OR in SQLAlchemy by importing _or from SQLAlchemy and then passing conditions on models.
from sqlalchemy import or_

session.query(
    UserModel.id,
    UserModel.first_name
).filter(
    or_(
        UserModel.username == 'john', 
        UserModel.username == 'michael'
    )
).all()
Code faster with devsheet chrome extension and increase productivity as a programmer
search code from your favorite search engine instantly.
Install Now

In the code snippet, we are applying OR condition on the username column where it will get the records if its username column has the value 'john' or 'michael'.

Was this helpful?
0 Comments
Open Code Manager We are hiring