Django modules中使用Q:or, and 多个组合同时存在时的查询
同时存在or, and组合查询时
实际查询中,会碰到or条件下的组合查询,而且每组条件还是and的多参数查询,此时需要用到Q
from django.db.models import Q
YourDBTable.objects.filter(
Q(StudentName='Name1', ClassRoom='#2') |
Q(StudentName='Name2', ClassRoom='#3') |
Q(StudentName='Name3', ClassRoom='#4')
)#.delete()
相当于:
select * from YourDBTable where
(StudentName='Name1' and ClassRoom='#2')
or(StudentName='Name2' and ClassRoom='#3')
or (StudentName='Name2' and ClassRoom='#4')
可见一个Q中可以同时设置多组参数。