SQLALchemy dynamic filter_by

前端 未结 2 469
温柔的废话
温柔的废话 2020-12-09 11:51

I know you can build dynamic filters for queries for SQLAlchemy by supplying **kwargs to filter_by.

For example

    filt         


        
2条回答
  •  盖世英雄少女心
    2020-12-09 12:04

    Instead of using filter_by I would recommend using filter, it gives you a lot more options.

    For example (from the manual):

    db.session.query(MyClass).filter(
        MyClass.name == 'some name',
        MyClass.id > 5,
    )
    

    In relation to your case:

    filters = (
        Transaction.amount > 10,
        Transaction.amount < 100,
    )
    db.session.query(Transaction).filter(*filters)
    

提交回复
热议问题