Django:按评论数量排序问题

发布于 2024-10-16 06:59:21 字数 1305 浏览 6 评论 0原文

我正在尝试根据 django 中的评论数量对项目列表进行排序。然而,似乎存在一个问题,即 Count 函数没有考虑到 django 注释还使用 content_type_id 来区分不同对象的注释这一事实!

这给我带来了一个小问题,即使用标准方法对所有对象的评论计数都是错误的;有一个“好的”修复还是我需要回到原始sql?

尝试获得正确排序的代码:

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

I'm trying to order a list of items in django by the number of comments they have. However, there seems to be an issue in that the Count function doesn't take into account the fact that django comments also uses a content_type_id to discern between comments for different objects!

This gives me a slight problem in that the comment counts for all objects are wrong using the standard methods; is there a 'nice' fix or do I need to drop back to raw sql?

Code to try and ge the correct ordering:

app_list = App.objects.filter(published=True)
.annotate(num_comments=Count('comments'))
.order_by('-num_comments')

Sample output from the query (note no mention of the content type 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-10-23 06:59:21

我想我找到了答案: Django Snippet

Think I found the answer: Django Snippet

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文