按相关对象的数量排序

时间:2011-10-12 15:05:35

标签: django google-app-engine django-views django-nonrel

我有下一个型号:

class Sentence(models.Model):
    text = models.CharField(max_length=200)       
    language = models.ForeignKey(Language)
    pub_date = models.DateTimeField()  

class Traduction(models.Model):
    sentence_from = models.ForeignKey(Sentence, related_name='st_traductions_from')
    sentence_to = models.ForeignKey(Sentence, related_name='st_traductions_to') 

我希望通过与它们相关的Traduction对象的数量来获得句子对象。我尝试过:

sentences = Sentence.objects.annotate(num_traductions=Count('st_traductions_from')) \
            .order_by('-num_traductions') 

但是当我迭代它时会引发下一个异常:

Caught DatabaseError while rendering: This query is not supported by the database

我正在使用appengine和Django-nonrel。

感谢。

1 个答案:

答案 0 :(得分:1)

请参阅:App Engine Datastore Viewer, how to show count of records using GQL?

GQL中没有计数聚合,即count(*)。有关替代方法,请参阅http://code.google.com/appengine/articles/sharding_counters.html