Django如何按天 小时等查询统计?
Posted junneyang 的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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://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
以上是关于Django如何按天 小时等查询统计?的主要内容,如果未能解决你的问题,请参考以下文章