2👍
✅
You can use Avg
to calculate an average:
score = Voto.objects.filter(look=look).aggregate(score=Avg('valor'))['score']
The reason your code doesn’t work, is because num_votos
is a queryset, and not a number. If you’d used score = valores['soma'] / num_votos.count()
it would’ve worked.
If you’re using Python 2, valores['soma'] / num_votos.count()
would still be an integer division and return an integer, not a real average. Use from __future__ import division
to convert it to ‘true division’, so it returns a float.
👤knbk
Source:stackexchange.com