How to make less or equal filter in Django queryset?

I am trying to filter users by custom field in each user profile named profile. This field is called the level and is an integer from 0 to 3.

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

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

When I try to filter using less:

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

I get an error message:

global name 'userprofile__level' undefined

Does the filter go to <or>, or am I barking the wrong tree.

+86
python django django-queryset
Apr 6 2018-12-12T00:
source share
1 answer

Less than or equal to:

 User.objects.filter(userprofile__level__lte=0) 

More or equal:

 User.objects.filter(userprofile__level__gte=0) 

Similarly, lt less and gt for more. You can find them in the documentation .

+163
Apr 6 2018-12-12T00:
source share



All Articles