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

509πŸ‘

βœ…

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/).

πŸ‘€lprsd

0πŸ‘

Put __gt suffix for "Greater Than" to the field name age:

Person.objects.filter(age__gt=20)
                    #    ↑↑↑↑ 
                    # age > 20

Put __gte suffix for "Greater Than or Equal to" to the field name age:

Person.objects.filter(age__gte=20)
                    #    ↑↑↑↑↑ 
                    # age >= 20

Put __lt suffix for "Less Than" to the field name age:

Person.objects.filter(age__lt=20)
                    #    ↑↑↑↑ 
                    # age < 20

Put __lte suffix for "Less Than or Equal to" to the field name age:

Person.objects.filter(age__lte=20)
                    #    ↑↑↑↑↑ 
                    # age <= 20

Leave a comment