[Answered ]-Get 1,2,3,4,5 star average individually (1star% +2star% +3star% +4star% +5star%=100%)

1👍

Create in your User model methods, that count the precentage and then simply use it as needed:

class User(...):
    ...
    def count_star_precentage(self, star):
        return (Rating.objects.filter(activity__creator=self, star=star).count() / self.all_ratings().count()) * 100

    def all_ratings(self):
        return Rating.objects.filter(activity__creator=self)

With them you can get precentage with simple call:

 user = User.objects.get(id=some_id)
 user.count_star_precentage(3)    # it will give the percent of total ratings with 3 star given by that user

Leave a comment