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.

推荐答案

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.

Python相关问答推荐

仿制药的类型铸造

通过Selenium从页面获取所有H2元素

如何在polars(pythonapi)中解构嵌套 struct ?

在线条上绘制表面

为什么默认情况下所有Python类都是可调用的?

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

如何使用pytest来查看Python中是否存在class attribution属性?

DataFrames与NaN的条件乘法

我想一列Panadas的Rashrame,这是一个URL,我保存为CSV,可以直接点击

cv2.matchTemplate函数匹配失败

Django admin Csrf令牌未设置

在pandas/python中计数嵌套类别

PYTHON、VLC、RTSP.屏幕截图不起作用

递归函数修饰器

jsonschema日期格式

如何使用Azure Function将xlsb转换为xlsx?

按条件添加小计列

PYTHON中的pd.wide_to_long比较慢

为什么在更新Pandas 2.x中的列时,数据类型不会更改,而在Pandas 1.x中会更改?

跨两个数据帧收集非索引列上的公共组