简体   繁体   中英

aggregate() + distinct(fields) not implemented

I have this code in my views.py, i just want to get the Average per Grading Categories

gradepercategory = studentsEnrolledSubjectsGrade.objects.filter(Grading_Categories__in = gradingcategories.values_list('id', flat=True)).filter(
                grading_Period__in=period.values_list('id', flat=True)).distinct('Grading_Categories').aggregate(Sum('Grade'))['Grade__sum']

admin-site image below to understand what i mean

在此处输入图片说明

In your code, you sum the distinct records. You can code like this

gradepercategory = studentsEnrolledSubjectsGrade.objects.filter(Grading_Categories__in = gradingcategories.values_list('id', flat=True)).filter(grading_Period__in=period.values_list('id', flat=True)).values('Grading_Categories').annotate(average_grade=Avg('Grade'))

You can check the link and get more information. https://docs.djangoproject.com/en/3.0/topics/db/aggregation/#aggregating-annotations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM