I have this rating for my review django model.
5 star 4 star 3 star 2 star 1 star
What I want to do is get the percentage of each component based on the rating. Something like this:
# get 5 average rating
five_avg_rating = qs.filter(rating=5).aggregate(Avg('rating'))['rating__avg']
five_avg_rating = (five_avg_rating / 5) * 100 if five_avg_rating != None else 0.0
# get 4 average rating
four_avg_rating = qs.filter(rating=4).aggregate(Avg('rating'))['rating__avg']
four_avg_rating = (four_avg_rating / 5) * 100 if four_avg_rating != None else 0.0
....
# get 1 average rating
one_avg_rating = qs.filter(rating=1).aggregate(Avg('rating'))['rating__avg']
one_avg_rating = (one_avg_rating / 5) * 100 if one_avg_rating != None else 0.0
The sum up percentages should = 100%. So if the math is correct, add all the rating percentages should = 100%
Here is an image of what I get now, which is not what I really want: enter image description here
How can I implement this better?
Doing the below seems to give me what I want! unless there is some better Django way of going about it.
# get 5 average rating
five_avg_rating = qs.filter(rating=5).aggregate(Avg('rating'))['rating__avg']
five_avg_rating = ((five_avg_rating * qs.filter(rating=5).count() / qs.count()) * 100) / 5 if five_avg_rating != None else 0.0