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?
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With