【Django】如何按天 小时等查询统计?

代码:

from django.db import connection
        from django.db.models import Sum,Count
        #alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filter(**condition).values('customer_id').annotate(alarm_sum=Sum('cmn_merge_count'))
        select = {'day': connection.ops.date_trunc_sql('day', 'cmn_alert_time')}
        alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filter(**condition).extra(select=select).values('day').annotate(sum=Sum('cmn_merge_count'))
        #serializer = serializers.FILE_PROTECT_ALARM_Serializer(file_protect_alarm_list, many=True)
        #data = {}
        #data['items'] = serializer.data
        '''for item in data['items']:
            item['svc_profile_list'] = json.loads(item['svc_profile_list'])
            #item['info'] = json.loads(item['info'])'''
        #data['total'] = total
        for item in alarm_sum_group_items:
            item['day'] = time.mktime(item['day'].timetuple())
        return APIResponse(status=status_code.success, data=alarm_sum_group_items)

 

推荐:

http://www.360doc.com/content/14/0430/17/16044571_373542312.shtml

http://stackoverflow.com/questions/8746014/django-group-by-date-day-month-year

http://stackoverflow.com/questions/3543379/django-annotate-groupings-by-month

 

参考资料:

http://www.360doc.com/content/14/0430/17/16044571_373542312.shtml

http://blog.csdn.net/shanliangliuxing/article/details/7927899

http://www.thinksaas.cn/ask/question/23626/

https://segmentfault.com/q/1010000003499386

http://blog.csdn.net/pipisorry/article/details/47396311

 

posted @ 2016-10-15 18:39  junneyang  阅读(1821)  评论(0编辑  收藏