notifications = DeviceNotification.objects.all()
for n in notifications:
time_period = n.time_period
logs = DeviceLog.objects.all()[:time_period]
for l in logs:
print(l.celsius)
我试图围绕如何发现返回的对象属性(让我们使用摄氏度)之间的温度“趋势”或“传播”。
基本上,我希望能够判断温度是上升了一定量还是下降了一定量......例如:if celsius has increased by 2 degrees within the past 5 hours
我不确定这是否可以通过过滤器完成,或者我是否必须将每个celsius
值添加到数组并将数组与abs()
或类似的数据进行比较。
答案 0 :(得分:0)
您可以通过将温度记录与时间段保持一致来实现。
假设你有温度记录,你想找出过去5小时内温度是否有所增加,
您可以在5小时之前获得温度值
log_now = TemperatureLog.objects.get(created=datetime.datetime.now()-timedelta(minutes=1))
log_past = TemperatureLog.objects.get(created=datetime.datetime.now()-timedelta(hours=5))
现在,你可以这样做,
if(log_now.celsius - log_past.celsius)> 1: 返回“温度上升2摄氏度” 其他: 返回“温度不增加2摄氏度”
答案 1 :(得分:0)
按照您的模式:
notifications = DeviceNotification.objects.all()
for n in notifications:
time_period = n.time_period
logs = DeviceLog.objects.all()[:time_period]
for l in logs:
print(l.celsius)
我将其更改为:
logs = DeviceLog.objects.all()
log_now = logs[datetime.datetime.now()].celsius
log_pre = logs[datetime.datetime.now() - 5].celsius
dif = log_now - log_pre
if (dif) >=0:
print ("increased: ",dif,"in 5 hours")
else:
print ("decreased: ",dif,"in 5 hours")