如何使用select
在views.py中选择or
操作符的一些列?我想在Django的views.py中运行以下查询:
SELECT name,family FROM student WHERE male = True OR degree=False
我使用了这个ORM:
student = stu.objects.filter(male = True).values('name','family') | stu.objects.filter(degree = False).values('name','family')
正确吗?是否有一种方法,我可以删除重复的values('name','family')
查询?
您可以使用Q对象:
https://docs.djangoproject.com/en/dev/topics/db/queries/complex-lookups-with-q-objects
from django.db.models import Q
student = stu.objects.filter(Q(male = True) | Q(degree = False)).values('name','family')