Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Averaging scores in database (Django)

Tags:

python

django

I have a database Result.objects.all() of around 15 objects. Result has a field called score which ranges from 1-5. So if I preform q = Result.objecets.get(id=1), q.score is 2. What's a method of finding the average of all scores for all 15 objects?

like image 576
ono Avatar asked Dec 11 '25 14:12

ono


1 Answers

You can use django aggregation functions to do this;

# Average price across all objects.
>>> from django.db.models import Avg
>>> Result.objects.all().aggregate(Avg('score'))
{'score__avg': 34.35}
like image 177
dm03514 Avatar answered Dec 13 '25 03:12

dm03514