23 Django--Q的使用
方式1:
models.xx.objects.filter(Q(id=10))
models.xx.objects.filter(Q(id=10)&Q(age=10) # and
models.xx.objects.filter(Q(id=10)|Q(age=10) # or
models.xx.objects.filter(Q(id__gt=10)|Q(age__lte=10) # or
models.xx.objects.filter(Q(Q(id__gt=10)|Q(age__lte=10)) & Q(name='lxx') # or
方式2:
from django.db.models import Q
conn = Q()
conn.connector = 'OR'
conn.children.append(('name__contains', '李杰'))
conn.children.append(('email', '李杰'))
conn.children.append(('id__gt', 5))
self.model_class.objects.filter(conn)
q1 = Q()
q1.connector = 'OR'
q1.children.append(('id', 1))
q1.children.append(('age', 10))
q2 = Q()
q2.connector = 'ADN'
q2.children.append(('size__gt', 10))
q2.children.append(('name', 'root'))
con = Q()
con.add(conn, 'ADN')
con.add(q2. 'ADN')
(id=1 or age=10) AND (size>10 and name=root)