如何在Django查询中执行OR过滤器? [英] How do I do an OR filter in a Django query?
本文介绍了如何在Django查询中执行OR过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够列出用户添加的项目(它们被列为创建者)或已被批准的项目。
I want to be able to list the items that either a user has added (they are listed as the creator) or the item has been approved.
所以我基本上需要选择:
So I basically need to select:
item.creator = owner or item.moderated = False
我如何在Django中做到这一点? (最好使用过滤器或查询集)。
How would I do this in Django? (preferably with a filter or queryset).
推荐答案
有 Q
对象复杂的查询。示例:
There is Q
objects that allow to complex lookups. Example:
from django.db.models import Q
Item.objects.filter(Q(creator=owner) | Q(moderated=False))
这篇关于如何在Django查询中执行OR过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文