0

我有一个如下查询,它返回特定会话(学年)中特定学期(学期)中特定班级的所有学生的成绩:

grades = Grade.objects.filter(term='First', student__in_class=1,session=1).order_by('-total')

然后另一个查询通过成绩注释以获得“总”字段的总和。

grades_ordered = grades.values('student')\
    .annotate(total_mark=Sum('total')) \
    .order_by('-total_mark')

起初一切正常,直到我从使用 SQLite 迁移到 postgreSQL,然后开始出现以下错误。

错误:

function sum(character varying) does not exist
LINE 1: SELECT "sms_grade"."student_id", SUM("sms_grade"."total") AS...
                                         ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

编辑:这是我的模型

class Grade(models.Model):
    session = models.ForeignKey(Session, on_delete=models.CASCADE)
    term = models.CharField(choices=TERM, max_length=7)
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    fca = models.CharField(max_length=10)
    sca = models.CharField(max_length=10)
    exam = models.CharField(max_length=10)
    total = models.CharField(max_length=3, blank=True, null=True)
    grade = models.CharField(choices=GRADE, max_length=1, blank=True, null=True)
    remark = models.CharField(max_length=50, blank=True, null=True)

您可以提供的任何帮助将不胜感激。

谢谢

4

1 回答 1

5

以整数或小数形式存储数字,而不是在 text/varchar 字段中

total = models.Integer(max_length=3, blank=True, null=True)

看到这个链接

也读这个

于 2019-02-21T12:14:34.710 回答