django 按天统计发布单数量

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)

 

posted @ 2023-04-15 13:03  super_ip  阅读(25)  评论(0编辑  收藏  举报