如何在自定义模型管理器中使用带有annotate()的order_by

时间:2011-08-20 08:38:08

标签: python django django-models

我有这些模特;有一个自定义经理。

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) 

我无法让自定义管理器工作.... 这没关系:

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 

这里出了什么问题?

我非常希望通过管理器执行此排序,或者至少可以从Entry实例中访问该方法,因为多个模板将使用此特殊排序(我不想将此复杂查询集复制到不同的视图)。

1 个答案:

答案 0 :(得分: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()

如果你甚至只需要注释来订购(这是我使用的情况),这是有道理的。