Python/Django — what is the difference between the “and” operator and the “&” operator

前端 未结 3 1216
[愿得一人]
[愿得一人] 2021-01-24 00:25

I have a interesting Django problem.

Consider the following:

Model.objects.filter(Q(id=\'test1\') and Q(id=\'test2\'))

this returns the

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-24 00:56

    I think you want OR here, and not AND

    filters = ['test1', 'test2', 'test3', 'test4']
    filtered = Model.objects.filter(Q(id = filters[0]))
    for f in filters[1:]:
         subset = Model.objects.filter(Q(id = f))
         filtered = filtered | subset
    

提交回复
热议问题