How to get past 6 days data in Django?

3238 views django

I have a variable reg_dateand devc. I want to get the total number of devc in the past 6 days.

I have these code in

todays_date =
past_six_days = todays_date - 6  ## I'm afraid if i do like this, I will get negative number when todays_date is 1/11/2018

How can I get the total number of device where 'reg_date' is inside the 'past_six_days' range ?

answered question

1 Answer


Assuming your model is called Device it would be something like:

from datetime import datetime, timedelta

today =
six_days_ago_date = (today - timedelta(days=6)).date()
Device.objects.filter(reg_date__gte=six_days_ago_date)  # all devices from past 6 days

posted this

Have an answer?


Please login first before posting an answer.