我有以下django查询集:
hero = DeedUser.objects\
.select_related('user')\
.values('user_id', 'user__display_name')\
.annotate(total_deed=Count('user_id'))
并返回以下内容:
SELECT "deeds_deeduser"."user_id", "users_user"."display_name", COUNT("deeds_deeduser"."user_id") AS "total_deed" FROM "deeds_deeduser" INNER JOIN "users_user" ON ( "deeds_deeduser"."user_id" = "users_user"."id" ) GROUP BY "deeds_deeduser"."user_id", "users_user"."display_name", "deeds_deeduser"."modified", "deeds_deeduser"."created" ORDER BY "deeds_deeduser"."modified" DESC, "deeds_deeduser"."created" DESC LIMIT 21
为什么它将字段已修改和已创建添加到 GROUP BY ,因为我已在select_related()中指定了该值
答案 0 :(得分:0)
我添加了
.order_by('total_deed')
现在有效。