0
djangoの項目のリストは、コメント数で注文しようとしています。しかし、Count関数は、djangoコメントがcontent_type_idを使って異なるオブジェクトのコメントを識別するという事実を考慮していないという問題があるようです。Django:コメント件数順に並べ替え
これは、標準的な方法を使ってすべてのオブジェクトのコメント数が間違っているという点で、私にはわずかな問題があります。 '良い'修正があるのですか、またはraw SQLに戻す必要がありますか?
正しい順序試してみて、GEのためのコード:クエリから
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