Django query filter combining AND and OR with Q objects don't return the expected results

前端 未结 4 1852
心在旅途
心在旅途 2021-02-05 01:51

I try to combine AND and OR in a filter using Q objects. It looks like that the | behave like an AND. This is related to the previous annotate which is run in the same query and

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 02:21

    This answer is late but could be helpful to a lot of guys out there.

    [...].filter(hide=False & deleted=False)
    .filter(Q(stock=False) | Q(quantity__gte=1))
    

    This will generate something similar to

    WHERE (hide=0 AND deleted=0 AND (T1.qty > 0 OR stock=0))
    

提交回复
热议问题