我是django的新手。我想创建一个包含一些从其他模型字段计算的字段的表。
class Student(models.Model):
name = models.Charfield(max_length = 30)
...
class Subject(models.Model):
student = models.ForeignKey(Student, related_name = "student_subject")
point = models.IntegerField(default = 0)
...
如何创建查询集以获得每个学生的平均分?
答案 0 :(得分:2)
相关文件是:https://docs.djangoproject.com/en/1.11/topics/db/aggregation/
from django.db.models import Avg
students = Student.objects.annotate(Avg('subject__point'))