我在Student对象上做了注释,因此它有两个新字段 - > project_count和member_count如下:
top_students = Student.objects.annotate(project_count= Count('project'), member_count = Count('member_student'))
我现在想在数据库级别执行这两个值的总和。返回类似的东西:
total_count = project_count + member_count
我尝试过像这样使用.extra():
top_students = Student.objects.annotate(project_count= Count('project'), member_count = Count('member_student')).extra(
select = {'total_count': 'project_count + member_count'},
order_by = ('total_count', )
)
但它显示错误:OperationalError: (1054, "Unknown column 'project_count' in 'field list'")
我应该编写原始SQL还是有其他方法来执行此操作: