0

非常に単純です...私はDjango 1.4.1を使用しており、コメントの数の逆数でクエリセットを注文する必要があります。私はDjangoコメントフレームワークを使用しており、他の回答で推奨されている .annotate(comment_count = Count('comment') 構造を使用しようとしました...「コメント」がフィールドエラーに解決されません。

また、データベース エラーをスローする django-generic-aggregate の 0.3.1 バージョンも試しました。

Photo.objects.filter(galleries=gallery).annotate(comment_count=Count('comments')).order_by('-comment_count')[(page-1)*RESULTS_PER_PAGE:page*RESULTS_PER_PAGE]

助言がありますか?

4

1 に答える 1

0

写真モデルの下に関数を置きます

class Photo(models.Model):
    .........

    def orders(self):
        //filter first the highest count of comments
        aggregate = Photo.objects.aggregate(comment_count=Count('comments'))
        return aggregate['comment_count'] + 1

次に、ビューで次のように呼び出すことができます。

comments = Photo.objects.filter(galleries=gallery).orders()
于 2013-03-11T02:53:56.347 に答える