[Answered ]-Filter people from Django model using birth date

2👍

You’ll have to check against a datetime.date that is the maximum allowed birth date: anyone born after that day will be younger than the min age:

from datetime import date

min_age = 24
max_date = date.today()
try:
    max_date = max_date.replace(year=max_date.year - min_age)
except ValueError: # 29th of february and not a leap year
    assert max_date.month == 2 and max_date.day == 29
    max_date = max_date.replace(year=max_date.year - min_age, month=2, day=28)
people = People.objects.filter(birth_date__lte=max_date)
👤knbk

Leave a comment