回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有这个模型,注释是用<code>django_comments</code>控件管理的:</p>
<pre><code>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'
)
</code></pre>
<p>我想检索<code>Fortune</code>对象,为每个对象添加一个<code>nb_comments</code>值,计算它们各自的注释数;我尝试以下查询:</p>
^{pr2}$
<p>从外壳上:</p>
<pre><code>>>> 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'}
</code></pre>
<p>以下是格式正确的sql查询:</p>
<pre><code>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
</code></pre>
<p>你能找出问题所在吗?Django不会使用<code>content_type</code>数据(其中包含对<code>fortune</code>的引用)来左连接<code>django_comments</code>表。在</p>
<p>这是我希望能够使用ORM生成的查询:</p>
<pre><code>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
</code></pre>
<p>但我做不到,所以我非常感谢Django退伍军人的帮助:)</p>
<p>提示:我使用的是django1.2-DEV</p>
<p>提前谢谢你的帮助。在</p>