次のコードがあり、django ビュー自体で平均データを取得する必要があります。しかし、変数の平均は常に定義されていません。何か案は?
cont_rating_tmp = EmployerReviewContractor.objects.filter(reviewed__id = cont.id).aggregate(average=Avg('avg_rate'))
cont.rate=average
次のコードがあり、django ビュー自体で平均データを取得する必要があります。しかし、変数の平均は常に定義されていません。何か案は?
cont_rating_tmp = EmployerReviewContractor.objects.filter(reviewed__id = cont.id).aggregate(average=Avg('avg_rate'))
cont.rate=average
クエリは辞書を返します。次の方法で値を取得できます。
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)