0

次のコードがあり、django ビュー自体で平均データを取得する必要があります。しかし、変数の平均は常に定義されていません。何か案は?

cont_rating_tmp = EmployerReviewContractor.objects.filter(reviewed__id = cont.id).aggregate(average=Avg('avg_rate'))

cont.rate=average
4

1 に答える 1

1

クエリは辞書を返します。次の方法で値を取得できます。

cont_rating_tmp = EmployerReviewContractor.objects.filter(
                         reviewed__id = cont.id
                            ).aggregate(average=Avg('avg_rate')) 

cont.rate=cont_rating_tmp["average"]

django 集計ドキュメントからサンプルをコピーします。

>>> from django.db.models import Max
>>> Book.objects.all().aggregate(Max('price'))
{'price__max': Decimal('81.20')}        <---  the result (a dictionary)
于 2012-07-13T14:42:13.453 に答える