How to get past 6 days data in Django?

3238 views django
1

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 views.py

todays_date = datetime.now()
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

7

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

from datetime import datetime, timedelta

today = datetime.now()
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?

JD

Please login first before posting an answer.