Flask SQLAlchemy filter by value OR another

前端 未结 3 1149
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 11:29

I have a Flask project that interacts with MySQL db through Flask-SQLAlchemy.

My question is, how to select a row from the database based on a v

3条回答
  •  梦毁少年i
    2021-01-31 12:19

    I also needed this case today, I found the nice answer here:

    So, we can make OR logic like the below example:

    from sqlalchemy import or_
    db.session.query(User).filter(or_(User.email=='useremail@example.com', User.name=="username")).first()
    

    When using the filter() expression, you must use proper comparison operators, whereas filter_by() uses a shortened unPythonic form.

提交回复
热议问题