在Django中通过SQL创建组的问题

时间:2014-12-17 22:21:46

标签: django django-views

我有以下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()中指定了该值

1 个答案:

答案 0 :(得分:0)

我添加了

.order_by('total_deed') 

现在有效。