2011-08-20 32 views
1

我有這些模型;有一個自定義管理器。如何在自定義模型管理器中使用帶註釋()的order_by

class OrderByHighestScoreManager(models.Manager): 
    def get_query_set(self, *args, **kwargs): 
     qs = super(OrderByHighestScoreManager, self).get_query_set(*args, **kwargs) 
     return qs.annotate(score=Count('votes'),).order_by('score') 

class AbstractEntry(models.Model): 
    user = models.ForeignKey(User, null=True, blank=True) 
    last_modified = models.DateTimeField(auto_now=True) 
    objects = models.Manager() 
    order_by_votes = OrderByHighestScoreManager() 

class Entry(AbstractEntry): 
    creation_date = models.DateTimeField(auto_now_add=True) 
    votes = generic.GenericRelation(Vote) 

我不能讓自定義管理工作.... 這是OK:

Entry.objects.annotate(score=Count('votes__id'),).order_by('score') 

這不起作用:

Entry.order_by_votes.all() 

我得到的錯誤是:

Traceback (most recent call last): 
File "c:\Python27\Lib\unittest\case.py", line 318, in run testMethod() 
File "C:\Data\Development\django_projects\oko\________\apps\entries\tests\model.py",line 111, in test_order_by_votes 
    self.assertEqual(list(Entry.order_by_votes.values_list('id', flat=True)), [self.e1.id, self.e2.id]) 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\query.py", line 84, in __len__ 
    self._result_cache.extend(self._iter) 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\query.py", line 956, in iterator 
    for row in self.query.get_compiler(self.db).results_iter(): 
File "C:\Data\Development\django_projects\oko\lib\site-packages \django\db\models\sql\compiler.py", line 680, in results_iter 
    for rows in self.execute_sql(MULTI): 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\sql\compiler.py", line 725, in execute_sql 
    sql, params = self.as_sql() 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\sql\compiler.py", line 60, in as_sql 
    ordering, ordering_group_by = self.get_ordering() 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\sql\compiler.py", line 349, in get_ordering 
    self.query.model._meta, default_order=asc): 
File "C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\sql\compiler.py", line 378, in find_ordering_name 
    opts, alias, False) 
File "C:\Data\Development\django_projects\oko\lib\site-packages \django\db\models\sql\query.py", line 1238, in setup_joins 
    "Choices are: %s" % (name, ", ".join(names))) 
    FieldError: Cannot resolve keyword 'score' into field. Choices are: 
    creation_date, id, last_modified, user, votes 

什麼是goi這裏錯了嗎?

我非常想通過一個管理器或至少一個可從Entry實例中訪問的方法進行排序,因爲幾個模板將使用此特殊排序(並且我不想複製這個複雜的查詢集不同的看法)。

+0

只是告訴人們我的進度:我發現將'order_by()'離開自定義管理器,並將其添加到我的視圖中的查詢集可以解決問題。儘管如此,它仍令我困惑。 – LaundroMat

回答

0

謝謝,你的迴應有助於理清我自己的,略有不同的方法。

class AbstractEntryManager(models.Manager): 
    def by_score(self): 
     qs = super(OrderByHighestScoreManager, self).get_query_set() 
     return qs.annotate(score=Count('votes')).order_by('score') 

class AbstractEntry(models.Model): 
    ... 
    objects = AbstractEntryManager() 

然後在您的視圖使用:Entry.objects.by_score()

此,如果你只甚至需要註釋的訂購(這是我使用的情況下)是有意義的。

相關問題