I want to compute the total average per grading categories and multiply it by given number using annotate
this is my views.py
from django.db.models import F
gradepercategory = studentsEnrolledSubjectsGrade.objects.filter(grading_Period = period).filter(Subjects = subject).filter\
(Grading_Categories__in = cate.values_list('id')).values('Grading_Categories').annotate( average_grade = Avg * F('Grading_Categories__PercentageWeight') / 100)
this is my models.py
class gradingCategories(models.Model):
CategoryName = models.CharField(max_length=500, null=True)
PercentageWeight = models.FloatField()
class studentsEnrolledSubjectsGrade(models.Model):
GradeLevel = models.ForeignKey(EducationLevel, related_name='grade', on_delete=models.CASCADE,
null=True, blank=True)
Subjects = models.ForeignKey(Subject, related_name='subject', on_delete=models.CASCADE, null=True)
Students_Enrollment_Records = models.ForeignKey(StudentsEnrolledSubject, related_name='student',
on_delete=models.CASCADE, null=True)
Grading_Categories = models.ForeignKey(gradingCategories, related_name='category', on_delete=models.CASCADE,
null=True, blank=True)
grading_Period = models.ForeignKey(gradingPeriod, related_name='period', on_delete=models.CASCADE,
null=True, blank=True)
_dates = models.CharField(max_length=255,null=True, blank=True)
Grade = models.FloatField(null=True, blank=True)
For more info:
For example the average of Quiz(Grading categories) is 80 and it will multiply with 15(PercentageWeight)
Total = 80(Grading_Categories) * 15(PercentageWeight) total=total / 100
The result must be 12
You can do this using F Expresisons
From the Docs:
Reporter.objects.all().update(stories_filed=F('stories_filed') + 1)
So your example would be something like:
studentsEnrolledSubjectsGrade.objects.all().annotate(grade_percent=(.8 * F('Grading_Categories')) * (.15 * F('PercentageWeight') / 100)
(as a side note, it is django/python standards to use UpperCaseCamelCase for class names, and snake_case for field names. So StudentsEnrolledSubjectsGrade, and grading_categories) check out Pep-8 for the details)
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.