How to do a less than or equal to filter in Django queryset?

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 away to filter by < or >, or am I barking up the wrong tree.

Answers


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.


Need Your Help

Is minus zero (-0) equivalent to zero (0) in C#

c#

Is minus zero (-0) equivalent to zero (0) in C#?

Do you follow the naming convention of the original programmer?

variables naming-conventions hungarian-notation

If you take over a project from someone to do simple updates do you follow their naming convention? I just received a project where the previous programmer used Hungarian Notation everywhere. Our...