from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import Sum,Max,Min,Avg
from grade.models import Grade, Student
""" 聚合及统计 """
# 计算张三的期末成绩总和
# grade_list = Grade.objects.filter(student_name='张三').aggregate(Sum('score'))
# grade_list = Grade.objects.filter(student_name='张三').aggregate(total = Sum('score'))
# 求语文最高分
# grade_list = Grade.objects.filter(subject_name='语文').aggregate(high_score=Max('score'))
# print(grade_list['high_score'])
# # 最低分
# grade_list = Grade.objects.filter(subject_name='语文').aggregate(min_score=Min('score'))
# print(grade_list['min_score'])
# 平均分
# grade_list = Grade.objects.filter(subject_name='语文').aggregate(avg_score=Avg('score'))
# print(grade_list['avg_score'])
# 统计每个学生的成绩总和
# total_list = Grade.objects.values_list('student_name').annotate(Sum('score'))
# for item in total_list:
# print(item)
# user_zhang = Student.objects.get(pk=1)
# list = user_zhang.stu_grade.all()
# for item in list:
# print(item.subject_name,item.score)
user_list = Student.objects.all().annotate(total_score = Sum('stu_grade__score'))
for user in user_list:
print(user.student_name,user.total_score)