Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I find how much 1 star/2 star—-5star present in percentage?

I’ve created a form for being stored ratings and feedback in the database. Ratings and Feedback are being stored in the database perfectly. But the problem is, I can’t find out how many different types of rating stars are present in the database. How can I find out how many 1 star/2star/—5 stars are present in the object model in percentage? What should I do It?

models.py:

class Frontend_Rating(models.Model):
    USer = models.ForeignKey(User,default=None,on_delete=models.CASCADE, related_name="frontend_rating")
    Rating = models.IntegerField(null=True)
    Feedback = models.TextField(max_length=250, null=True)

    def __str__(self):
        return str(self.pk)+ str(".") + str(self.USer) + str("(") + str(self.Rating) + str("stars") +str(")")

Views.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def index(request):
    
    #rating____________________
    frontend_all_ratings = Frontend_Rating.objects.all()
    number_of_frontend_rating = frontend_all_ratings.count()
    average_rating = 0

    frontend_one_star = 0
    frontend_two_star = 0
    frontend_three_star = 0
    frontend_four_star = 0
    frontend_five_star = 0

    percentage_frontend_ONE_star = 0
    percentage_frontend_FIVE_star = 0


    for frontend_rating_item in frontend_all_ratings:
        frontend_rating = frontend_rating_item.Rating

        if frontend_rating:
            total_ratings = 0
            total_ratings += frontend_rating
            average_rating = round(total_ratings/frontend_all_ratings.count(),1)

        
    context = {
        "frontend_five_star":frontend_five_star,
        "frontend_one_star":frontend_one_star,
        "total_ratings":total_ratings,
        "average_rating":average_rating,
    }
    return render(request,'0_index.html',context)

>Solution :

number_of_frontend_rating = Frontend_Rating.objects.count()

# Divide value by overall count to get ratio and multiply ratio by 100 to get percentage
frontend_one_star = (Frontend_Rating.objects.filter(Rating=1).count() / overall_count)*100
frontend_two_star = (Frontend_Rating.objects.filter(Rating=2).count() / overall_count)*100
frontend_three_star = (Frontend_Rating.objects.filter(Rating=3).count() / overall_count)*100
frontend_four_star = (Frontend_Rating.objects.filter(Rating=4).count() / overall_count)*100
frontend_five_star = (Frontend_Rating.objects.filter(Rating=5).count() / overall_count)*100

Please correct me if I misunderstood your question

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading