FLASK-SQLALCHEMY and conditions and how to use or combine query

FLASK-SQLALCHEMY and conditions and how to use or combine query

http://www.cherishlau.site/2018/03/29/flask-sqlalchemy-use-or-and/

Recent work management system with a Flask development, ORM framework database using Flask-SQLAlchemy, Flask-SQLAlchemy simple and convenient to use, basic usage can be found in the official document above, but when I met with a filter of some problem.

 

How to use the OR and AND in the FILTER FLASK-SQLALCHEMY to combine a variety of conditions in the query?

Find documents related to the time and description is not found, then after several attempts, we found that can be written like this:

Import-related first of all to library

from flask_sqlalchemy import SQLAlchemy

from sqlalchemy import or_,and_

Then define filter:

task_filter = {
  or_(
   and_(
 Task.task_department == current_user.user_department,  Task.task_commit_status > 0,  Task.task_complete_time >= prev, ), and_(  Task.task_complete_time >= prev,  Task.task_members.like('%,'+str(current_user.user_id)+',%'), ) ) } 

There are two above the filter condition group, relationship or, for each set of conditions, there are a number of conditions and relationships.

After defining the filter can be inquired:

Task.query.filter(*task_filter).all()

Guess you like

Origin www.cnblogs.com/shengs/p/12074737.html