如何做一个小于或等于过滤Django查询? [英] How to do a less than or equal to filter in Django queryset?

查看:278
本文介绍了如何做一个小于或等于过滤Django查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过称为个人资料的每个用户个人资料中的自定义字段过滤用户。该字段被称为级别,是0-3之间的整数。

I am attempting to filter users by a custom field in each users profile called profile. This field is called level and is an integer between 0-3.

如果我使用equals进行过滤,我将获得所需级别的用户列表:

If I filter using equals, I get a list of users with the chosen level as expected:

user_list = User.objects.filter(userprofile__level = 0)

当我尝试过滤使用少于:

When I try to filter using less than:

user_list = User.objects.filter(userprofile__level < 3)

我收到错误:


全局名称'userprofile__level'未定义

global name 'userprofile__level' is not defined

有没有过滤器或>,或者我正在树上错误的树。

Is there away to filter by < or >, or am I barking up the wrong tree.

推荐答案

小于或等于:

User.objects.filter(userprofile__level__lte=0)

大于或等于:

User.objects.filter(userprofile__level__gte=0)

同样地, lt 少于 gt 大于。您可以在文档中找到所有

Likewise, lt for less than and gt for greater than. You can find them all in the documentation.

这篇关于如何做一个小于或等于过滤Django查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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