如何在 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屋!
查看全文