2011-08-20 50 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()

此,如果你只甚至需要注释的订购(这是我使用的情况下)是有意义的。

相关问题