班级&用于显示Max&的视图Django中每天的平均值

时间:2018-04-11 07:05:54

标签: python django sqlite

我是python(和django)的新手,我搜索过但老实说我不确定我是否正在寻找合适的东西,所以决定寻求帮助。

这是我的班级。我有一个脚本设置,每5分钟输入一次并将这些值保存到数据库中。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

我想设置一个类/视图来显示日期,'内部'和&的平均值(和高)那天的“外面”。当天gavg的平均值。那天'ghigh'的高点。这需要在Entry课程中每一天完成。

我试图了解我需要在ORM中做些什么,但是遇到了时区感知日期时间问题。我猜我需要另一个类,并且我已经看到了一些可能做我需要的功能但是我很难想象如何定义类以及平均/高功能应该如何/在哪里。或者这更适合作为SQL查询?

3 个答案:

答案 0 :(得分:1)

查看Celery,您可以运行一个计划,每天更新数据库中的所有对象。

from datetime import timedelta
from django.utils import timezone

def update_previous_day_entires():
    current_day = timezone.now()
    previous_day = current_day - timedelta(hours=24)
    entries = Entry.objects.filter(time__gt=previous_day,time__lt=current_day)

    # Calculate average and high here

    Entry.objects.filter(time__gt=previous_day,time__lt=current_day).update(
    gavg=avg,ghigh=high)

答案 1 :(得分:1)

您可以获得如下聚合结果,

from django.db.models import Avg, Max

aggrate_result = Entry.objects.filter(time=filter_date).aggregate(avg_inside=Avg('inside'), avg_outside=Avg('outside'),
                                                                  max_inside=Max('inside'), max_outside=Max('outside'))


您应该阅读official documentaion of django了解有关重新Aggregation的详细信息。
在您的视图中,您可以显示汇总的响应,如下所示,

from django.http import JsonResponse
from django.db.models import Avg, Max


def my_sample_view(request):
    aggrate_result = Entry.objects.filter(time=filter_date).aggregate(avg_inside=Avg('inside'), avg_outside=Avg('outside'),
                                                                      max_inside=Max('inside'), max_outside=Max('outside'))
    return JsonResponse(aggrate_result)


注意:你应该通过official doc来获得这些东西。快乐学习;)

答案 2 :(得分:0)

感谢你们俩。我结合使用两者来获得我需要的东西。

from datetime import timedelta
from django.utils import timezone
from django.db.models import Avg, Max
from temp.models import *
import temp.views
import django

def update_avg(self, start, end):
    entries_for_period = Entry.objects.filter(time__gte=start, time__lte=end)
    avg_values = ['inside', 'outside', 'gavg']
    average_value = entries_for_period.aggregate(*[Avg(x) for x in avg_values])
    return {x:average_value['%s__avg' % x] for x in avg_values}



def update_high(self, start, end):
    entries_for_period2 = Entry.objects.filter(time__gte=start, time__lte=end)
    high_values = ['inside', 'outside', 'ghigh']
    high_value = entries_for_period2.aggregate(*[Max(x) for x in high_values])
    return {x:high_value['%s__max' % x] for x in high_values}

current_day = timezone.now()    
previous_day = current_day - timedelta(hours=24)
date_entry = previous_day.date()

avg_choice = update_avg(date_entry,previous_day,current_day)
high_choice = update_high(date_entry,previous_day,current_day)

history = History(date=date_entry, avginside=round(avg_choice['inside'],2),highinside=round(high_choice['inside'],2),avgoutside=round(avg_choice['outside'],2),highoutside=high_choice['outside'],avgg=avg_choice['gavg'],highg=high_choice['ghigh'],starttime=previous_day,endtime=current_day)
history.save()

现在我可以获得模型输入的平均值和高值的每日快照。谢谢!