models.py
class Preschooler(Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
birthday = models.DateField(null=True, blank=True)
def age_months(self):
today = date.today()
date_diff = today - self.birthday
in_days = date_diff.days
return int((in_days) / (365 / 12))
I want to filter preschooler that are less than 60 months using their birthday like this:
Preschooler.objects.filter(birthday__month__lst=60)
How can I achieve this?
CodePudding user response:
You can use Python relativedelta
with datetime.today()
[Python-doc] so:
from datetime import datetime
from dateutil.relativedelta import relativedelta
less_than_60_mon = datetime.today() - relativedelta(months=60)
Preschooler.objects.filter(birthday__lt=less_than_60_mon)