django orm按天统计发布单数量

夜深了,先上代码和数据,明天再实现可视化图表。

from datetime import datetime, timedelta
from django.http import JsonResponse
from deploy.models import DeployPool
from django.db.models import Count


def get_a_month_deploy(request):
    return_list = []
    now = datetime.now()
    a_month = now - timedelta(days=50)
    select = {'day': 'date(add_date)'}
    a_month_deploy = DeployPool.objects. \
        filter(add_date__range=(a_month, now)). \
        extra(select=select).values('day').distinct().order_by("day").annotate(number=Count('add_date'))

    for item in a_month_deploy:
        item_dict = {}
        item_key = item['day'].strftime('%m-%d')
        item_dict[item_key] = item['number']
        return_list.append(item_dict)

    return JsonResponse(return_list, safe=False)

django orm按天统计发布单数量_第1张图片

转载于:https://www.cnblogs.com/aguncn/p/10051639.html

你可能感兴趣的:(django orm按天统计发布单数量)