[Django]-Python – Django – Filter and count all people in first year from start date

3πŸ‘

βœ…

Since there is not an end_date, you can not filter much on that one. What you can do is simply set the start_date to be greater than or equal to now minus 365 days:

total_people = Contracts.objects.filter(
    start_date__gte=timezone.now() - timedelta(days=365)
).count()

A year however is not per se 365 days. You can for example make use of the python-dateutil package [pypi], and work with a relativedelta [readthedocs.io]:

from dateutil.relativedelta import relativedelta

total_people = Contracts.objects.filter(
    start_date__gte=timezone.now() - relativedelta(years=1)
).count()

Leave a comment