SQLAlchemy - “Dynamic Filter”

后端 未结 3 1759
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 17:22

I have just started using SQLAlchemy. I decided to use it because I was using a lot of string expression in the middle of my sqlite queries.

So, that is my problem.

3条回答
  •  情书的邮戳
    2020-12-06 17:53

    I assume you are using the ORM.

    in that case, the filter function returns a query object. You can conditionaly build the query by doing something like

    query = Session.query(schema.Object).filter_by(attribute=value)
    if condition:
        query = query.filter_by(condition_attr=condition_val)
    if another_condition:
        query = query.filter_by(another=another_val)
    
    #then finally execute it
    
    results = query.all()
    

提交回复
热议问题