Home > database >  django filter gte and lte on one property of one model
django filter gte and lte on one property of one model

Time:03-16

I have a User model with age property

my models.py

class User(models.Model):
    age = models.IntegerField()

i need to output all users between age 25 and 35

i can only make query which won't exclude others users

first_query = User.objects.filter(age__gte=25)
second_query = User.objects.filter(age__lte=35)

can i output users between age 25 and 35 and exclude others, in one query?

CodePudding user response:

try this

User.objects.filter(age__gte=25, age__lte=35)

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#gte

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#lte

  • Related