django orm 跨表查询定义key

正常查询SQL

cpu_mem = models.Instances.objects.values(
'owner__department_2').annotate(
                              cpu=Sum('cpu_core'),
                              mem=Sum('memory'),
                              count=Count('instance_id')
    

返回结果

{'owner__department_2': '行业产品', 'cpu': 112, 'mem': 256, 'count': 25}
{'owner__department_2': '华南交付中心', 'cpu': 138, 'mem': 356, 'count': 34}
{'owner__department_2': '解决方案组', 'cpu': 48, 'mem': 168, 'count': 8}
{'owner__department_2': '华东交付中心', 'cpu': 38, 'mem': 128, 'count': 11}
{'owner__department_2': '产品架构', 'cpu': 6, 'mem': 32, 'count': 3}

使用F定义返回的KEY

cpu_mem = models.Instances.objects.values(
dept=F('owner__department_2')).annotate(
                              cpu=Sum('cpu_core'),
                              mem=Sum('memory'),
                              count=Count('instance_id')

返回结果

{'dept': '行业产品', 'cpu': 112, 'mem': 256, 'count': 25}
{'dept': '华南交付中心', 'cpu': 138, 'mem': 356, 'count': 34}
{'dept': '解决方案组', 'cpu': 48, 'mem': 168, 'count': 8}
{'dept': '华东交付中心', 'cpu': 38, 'mem': 128, 'count': 11}
{'dept': '产品架构', 'cpu': 6, 'mem': 32, 'count': 3}
posted @ 2020-07-16 10:36  daniell  阅读(94)  评论(0编辑  收藏  举报