代码示例:
from django.db.models import Sum, Count #alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filter(**condition).filter(device_hash=tmp_dict['device_hash']).values('device_hash').annotate(alarm_sum=Sum('cmn_merge_count')) #tmp_dict['customer_id'] = customer_id #tmp_dict['alarm_sum'] = alarm_sum_group_items[0]['alarm_sum'] if (len(alarm_sum_group_items)!=0) else 0 alarm_sum = models.FILE_PROTECT_ALARM.objects.filter(**condition).filter(device_hash=tmp_dict['device_hash']).aggregate(Sum("cmn_merge_count")) # { 'cmn_merge_count__sum': None} tmp_dict['alarm_sum'] = alarm_sum['cmn_merge_count__sum'] if (alarm_sum['cmn_merge_count__sum'] is not None) else 0 rule_sum = models.FILE_PROTECT_RULE_UPDATE.objects.filter(customer_id=customer_id).filter(device_hash=tmp_dict['device_hash']).aggregate(Count("id")) # { 'id__count': 0} tmp_dict['rule_sum'] = rule_sum['id__count']