2010-05-02 98 views
2

我有這個模型中,註釋與django_comments的contrib管理的連接使用`comments`的contrib:問題努力實現在Django

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')) 

從貝:

>>> 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

在此先感謝您的幫助。

回答

2

爲什麼你想加入內容類型表?原始查詢是錯誤的,但不是因爲這個原因。對內容類型的引用是爲了確定註釋與哪個目標模型相關聯 - 但您已經知道,因爲您只選擇與Fortune相關聯的註釋。 Django會已質疑了ContentType模型以獲取Fortune的價值,所以應該簡單地增加一個WHERE條款:通過增加ORM相當於只是適當

... WHERE django_comments_comment.content_type_id = xx 

您可能能夠使這項工作:

...filter(comment__content_type=ContentType.objects.get_for_model(Fortune)) 

雖然它似乎是Django沒有自動執行此操作的錯誤。

+0

謝謝你,通過更換通過''comment__content_type'它comments__content_type'工作,所以謝謝你一次。 順便說一句,cyberdelia說,這確實是一個[bug](http://code.djangoproject.com/changeset/10781):/ – NiKo 2010-05-02 18:43:19

+0

呃,對不起,實際上它沒有工作。我結束了不正確的評論計數:/ – NiKo 2010-05-02 19:24:53

2

這是Django中的一個錯誤,請參閱#10870。你必須至少等待Django 1.3或者自己修復它。

+0

是的,感謝指針,它幫助。 – NiKo 2010-05-02 18:43:41