如何在Django中获取过去6天的数据?

时间:2018-11-09 01:44:43

标签: django

我有一个变量reg_datedevc。 我想在过去6天内获得total number of devc

我在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

如何获取“ reg_date”在“ past_six_days”范围内的设备总数?

1 个答案:

答案 0 :(得分:3)

假设您的模型名为Device,则类似于:

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