问题:如何在Django queryset中执行小于或等于过滤器?

我试图通过每个称为“个人资料”的用户个人资料中的自定义字段来过滤用户。此字段称为级别,是0到3之间的整数。

如果我使用等于进行过滤,则会得到具有预期级别的用户列表:

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

当我尝试使用少于以下内容进行过滤时:

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

我得到了错误:

未定义全局名称“ userprofile__level”

有没有一种方法可以通过<或>进行过滤,或者我是否吠叫了错误的树。

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.

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)

I get the error:

global name ‘userprofile__level’ is not defined

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


回答 0

小于或等于:

User.objects.filter(userprofile__level__lte=0)

大于或等于:

User.objects.filter(userprofile__level__gte=0)

同样,lt小于和gt大于。您可以在文档中找到它们。

Less than or equal:

User.objects.filter(userprofile__level__lte=0)

Greater than or equal:

User.objects.filter(userprofile__level__gte=0)

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。