I have a form which allows a user to select users by multiple age ranges: 18-24, 25-34, 35-44 etc. Currently if a user selects the first two fields in the multiple selection list, the query will return users 18 - 34. If a user selects 18 - 24 year olds, and 35-44 year olds, it will include all ages from 18 - 44, when it should not include users aged 25-34.
How would you approach including multiple range filters on a query without knowing the number of ranges you need to filter by prior to the users form selections?
If you knew the number of age ranges the user was going to filter by I know you could chain range queries using Q.
This is my current query:
query = User.objects.filter(
gender__in=genders,
created__in=dates,
data_source__in=sources,
dob__range=[dob_from, dob_to]
)
Thanks in advance.