">", "<", ">=" and "<=" don't work with "filter()" in Django

django, django-filter, django-queryset, inequality, python

Solution

Greater than:

Person.objects.filter(age__gt=20)

Greater than or equal to:

Person.objects.filter(age__gte=20)

Less than:

Person.objects.filter(age__lt=20)

Less than or equal to:

Person.objects.filter(age__lte=20)

You can find them all in [the documentation].(https://docs.djangoproject.com/en/stable/ref/models/querysets/).

Problem

With `=` below, I could filter persons by `age`: ``` qs = Person.objects.filter(age = 20) # ↑ Here ``` But with `>`, `<`, `>=` and `<=` below, I couldn't filter persons by `age`: ``` qs = Person.objects.filter(age > 20) # ↑ Here ``` ``` qs = Person.objects.filter(age < 20) # ↑ Here ``` ``` qs = Person.objects.filter(age >= 20) # ↑↑ Here ``` ``` qs = Person.objects.filter(age <= 20) # ↑↑ Here ``` Then, I got the error below: NameError: name 'age' is not defined How can I do `greater than(>)`, `greater than or equal to(>=)`, `less than(<)` and `less than or equal to(>=)` with filter() in Django?

Original source