Django:按评论数量排序
我正试图按评论的数量来排列django中的项目列表。然而,似乎有一个问题,Count函数没有考虑到django注释也使用content_type_id来区分不同对象的注释 这给了我一个小问题,使用标准方法时,所有对象的注释计数都是错误的;是否有一个“好的”修复程序,或者我需要回到原始sql 尝试并确定正确顺序的代码:Django:按评论数量排序,django,django-comments,Django,Django Comments,我正试图按评论的数量来排列django中的项目列表。然而,似乎有一个问题,Count函数没有考虑到django注释也使用content_type_id来区分不同对象的注释 这给了我一个小问题,使用标准方法时,所有对象的注释计数都是错误的;是否有一个“好的”修复程序,或者我需要回到原始sql 尝试并确定正确顺序的代码: app_list = App.objects.filter(published=True) .annotate(num_comments=Count('comments')) .o
app_list = App.objects.filter(published=True)
.annotate(num_comments=Count('comments'))
.order_by('-num_comments')
查询注释的示例输出未提及内容类型id:
SELECT "apps_app"."id", "apps_app"."name",
"apps_app"."description","apps_app"."author_name", "apps_app"."site_url",
"apps_app"."source_url", "apps_app"."date_added", "apps_app"."date_modified",
"apps_app"."published", "apps_app"."published_email_sent", "apps_app"."created_by_id",
"apps_app"."rating_votes", "apps_app"."rating_score", COUNT("django_comments"."id") AS
"num_comments" FROM "apps_app" LEFT OUTER JOIN "django_comments" ON ("apps_app"."id" =
"django_comments"."object_pk") WHERE "apps_app"."published" = 1 GROUP BY
"apps_app"."id", "apps_app"."name", "apps_app"."description", "apps_app"."author_name",
"apps_app"."site_url", "apps_app"."source_url", "apps_app"."date_added",
"apps_app"."date_modified", "apps_app"."published", "apps_app"."published_email_sent",
"apps_app"."created_by_id", "apps_app"."rating_votes", "apps_app"."rating_score" ORDER
BY num_comments DESC LIMIT 4
我想我找到了答案: