我有一个模型,评论是用django_comments控件来管理的:
class Fortune(models.Model):
author = models.CharField(max_length=45, blank=False)
title = models.CharField(max_length=200, blank=False)
slug = models.SlugField(_('slug'), db_index=True, max_length=255, unique_for_date='pub_date')
content = models.TextField(blank=False)
pub_date = models.DateTimeField(_('published date'), db_index=True, default=datetime.now())
votes = models.IntegerField(default=0)
comments = generic.GenericRelation(
Comment,
content_type_field='content_type',
object_id_field='object_pk'
)我想要检索Fortune对象,每个对象都有一个补充的nb_comments值,计算它们各自的注释数量;我尝试执行以下查询:
>>> Fortune.objects.annotate(nb_comments=models.Count('comments'))在shell中:
>>> from django_fortunes.models import Fortune
>>> from django.db.models import Count
>>> Fortune.objects.annotate(nb_comments=Count('comments'))
[<Fortune: My first fortune, from NiKo>, <Fortune: Another One, from Dude>, <Fortune: A funny one, from NiKo>]
>>> from django.db import connection
>>> connection.queries.pop()
{'time': '0.000', 'sql': u'SELECT "django_fortunes_fortune"."id", "django_fortunes_fortune"."author", "django_fortunes_fortune"."title", "django_fortunes_fortune"."slug", "django_fortunes_fortune"."content", "django_fortunes_fortune"."pub_date", "django_fortunes_fortune"."votes", COUNT("django_comments"."id") AS "nb_comments" FROM "django_fortunes_fortune" LEFT OUTER JOIN "django_comments" ON ("django_fortunes_fortune"."id" = "django_comments"."object_pk") GROUP BY "django_fortunes_fortune"."id", "django_fortunes_fortune"."author", "django_fortunes_fortune"."title", "django_fortunes_fortune"."slug", "django_fortunes_fortune"."content", "django_fortunes_fortune"."pub_date", "django_fortunes_fortune"."votes" LIMIT 21'}下面是格式正确的sql查询:
SELECT "django_fortunes_fortune"."id",
"django_fortunes_fortune"."author",
"django_fortunes_fortune"."title",
"django_fortunes_fortune"."slug",
"django_fortunes_fortune"."content",
"django_fortunes_fortune"."pub_date",
"django_fortunes_fortune"."votes",
COUNT("django_comments"."id") AS "nb_comments"
FROM "django_fortunes_fortune"
LEFT OUTER JOIN "django_comments"
ON ("django_fortunes_fortune"."id" = "django_comments"."object_pk")
GROUP BY "django_fortunes_fortune"."id",
"django_fortunes_fortune"."author",
"django_fortunes_fortune"."title",
"django_fortunes_fortune"."slug",
"django_fortunes_fortune"."content",
"django_fortunes_fortune"."pub_date",
"django_fortunes_fortune"."votes"
LIMIT 21你能发现问题所在吗?Django不会将django_comments表与content_type数据(其中包含对fortune表的引用)连接在一起。
这是我希望能够使用ORM生成的查询:
SELECT "django_fortunes_fortune"."id",
"django_fortunes_fortune"."author",
"django_fortunes_fortune"."title",
COUNT("django_comments"."id") AS "nb_comments"
FROM "django_fortunes_fortune"
LEFT OUTER JOIN "django_comments"
ON ("django_fortunes_fortune"."id" = "django_comments"."object_pk")
LEFT OUTER JOIN "django_content_type"
ON ("django_comments"."content_type_id" = "django_content_type"."id")
GROUP BY "django_fortunes_fortune"."id",
"django_fortunes_fortune"."author",
"django_fortunes_fortune"."title",
"django_fortunes_fortune"."slug",
"django_fortunes_fortune"."content",
"django_fortunes_fortune"."pub_date",
"django_fortunes_fortune"."votes"
LIMIT 21但我不能做到这一点,所以Django老兵的帮助是非常感谢的:)
提示:我正在使用Django 1.2-DEV
提前感谢您的帮助。
发布于 2010-05-25 01:04:16
http://charlesleifer.com/blog/generating-aggregate-data-across-generic-relations/ http://djangosnippets.org/snippets/2034/
更新:http://github.com/coleifer/django-generic-aggregation/
发布于 2010-05-03 02:01:53
为什么要加入内容类型表?原始查询是错误的,但不是因为这个原因。对内容类型的引用是为了标识注释与哪个目标模型相关联--但是您已经知道这一点,因为您只选择了与Fortune相关联的注释。Django已经查询了ContentType模型来获取Fortune的值,所以应该只添加一个WHERE子句:
... WHERE django_comments_comment.content_type_id = xx您也许可以通过添加与之等效的ORM来使其正常工作:
...filter(comment__content_type=ContentType.objects.get_for_model(Fortune))尽管Django没有自动完成这一点,但这看起来确实是一个错误。
发布于 2010-05-03 02:36:23
这是Django中的一个bug,请参阅ticket #10870。你至少要等到Django 1.3,或者自己修复它。
https://stackoverflow.com/questions/2754320
复制相似问题