首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试在Django中使用`comments` contrib实现联接时出现问题

尝试在Django中使用`comments` contrib实现联接时出现问题
EN

Stack Overflow用户
提问于 2010-05-03 01:39:34
回答 3查看 365关注 0票数 3

我有一个模型,评论是用django_comments控件来管理的:

代码语言:javascript
复制
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值,计算它们各自的注释数量;我尝试执行以下查询:

代码语言:javascript
复制
>>> Fortune.objects.annotate(nb_comments=models.Count('comments'))

在shell中:

代码语言:javascript
复制
>>> 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查询:

代码语言:javascript
复制
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生成的查询:

代码语言:javascript
复制
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

提前感谢您的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 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/

票数 3
EN

Stack Overflow用户

发布于 2010-05-03 02:01:53

为什么要加入内容类型表?原始查询是错误的,但不是因为这个原因。对内容类型的引用是为了标识注释与哪个目标模型相关联--但是您已经知道这一点,因为您只选择了与Fortune相关联的注释。Django已经查询了ContentType模型来获取Fortune的值,所以应该只添加一个WHERE子句:

代码语言:javascript
复制
... WHERE django_comments_comment.content_type_id = xx

您也许可以通过添加与之等效的ORM来使其正常工作:

代码语言:javascript
复制
...filter(comment__content_type=ContentType.objects.get_for_model(Fortune))

尽管Django没有自动完成这一点,但这看起来确实是一个错误。

票数 2
EN

Stack Overflow用户

发布于 2010-05-03 02:36:23

这是Django中的一个bug,请参阅ticket #10870。你至少要等到Django 1.3,或者自己修复它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2754320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档