Django过滤器 - 还是? [英] Django Filters - or?

查看:128
本文介绍了Django过滤器 - 还是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在django过滤器中执行或。

How would I do an "or" in a django filter.

基本上,我希望能够列出用户添加的项目(他们被列为创建者)或该项目已被批准

Basically, 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

所以我基本上需要选择

item.creator = owner or item.moderated = False

在django中执行此操作(最好使用过滤器/查询器)

How would I do this in django (preferably with a filter/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过滤器 - 还是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆