【发布时间】:2021-01-30 11:08:17
【问题描述】:
我正在计算每个部门的分数总和以及注释中每个部门的用户数,如下所示:
queryset = Depts.objects.annotate(total_pts=Sum('userprofiles__user__activity_user__activity_category__pts'), total_users=Count('userprofiles', distinct=True)).order_by('-total_pts')
我还想作为返回数据的一部分发回 avg_score = total_pts / total_users。有没有办法将它添加到注释中,或者将它们链接在一起,或者以其他方式将它添加到返回查询集中?
编辑:
我尝试添加如下内容:
queryset = Depts.objects.annotate(total_pts=Sum('userprofiles__user__activity_user__activity_category__pts'), total_users=Count('userprofiles', distinct=True), avg_score=total_pts/total_users).order_by('-total_pts')
错误提示未定义 total_pts。
谢谢。
【问题讨论】:
-
当前设置有什么问题?有错误吗?什么错误?也添加一个最小可验证示例
标签: django django-rest-framework django-orm