我想在django中使用带OR条件的filter来过滤对象数据。
我有一个类的对象,我有3到4个字段,我想在Django中用OR条件过滤数据。
例如。
Obj = Books.objects.filter(title=title or price=price or description=description or author=author)我认为这不是执行filter的正确方式。
在我的django过滤器中使用OR条件的正确方式是什么
发布于 2016-10-05 18:23:17
from django.db.models import Q
Obj = Books.objects.filter(Q(title=title) | Q(price=price) | Q(description=description) | Q(author=author))文档:http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects来源:https://stackoverflow.com/a/739922/4808939
https://stackoverflow.com/questions/39871370
复制相似问题