简体   繁体   中英

how can I display the average rating as stars?

I'm working on my web app and I want to display the average rating to doctors but when I tried the avg and created a function in my model it just showed me the last rate, not the average

this is my model

class Doctor(models.Model):
    user = models.OneToOneField(
        User, on_delete=models.CASCADE, primary_key=True)
    number_phone = models.CharField(
        _('االهاتف :'), max_length=50, blank=True, null=True)

  def averagreview(self):

        reviews = Comments.objects.filter(
            doctor=self).aggregate(average=Avg('rating'))
       
        avg = 0
        if reviews["average"] is not None:
            avg = float(reviews["average"])
            return avg

    def countreviews(self):
        reviews = Comments.objects.filter(
            doctor=self).aggregate(count=Count('id'))
        cnt = 0
        if reviews["count"] is not None:
            cnt = int(reviews["count"])
        return cnt

this the Comments model 

class Comments(models.Model):
    doctor = models.ForeignKey(
        Doctor, on_delete=models.CASCADE,   related_name='comment')
    created_by = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='comments')
    # co_email = models.ForeignKey(
    #     User, on_delete=models.CASCADE, related_name='comment')
    review = models.TextField(max_length=400, verbose_name='التعليق')
    rating = models.IntegerField(default=0)

this is my HTML


<div class="rating">
                                    <div class="text">
                                        <span class="rating-star">
                                            <i class="fa fa-star{% if doctor.averagreview < 1%}-o empty checked{% endif %}"
                                                aria-hidden="true"></i>
                                            <i class="fa fa-star{% if doctor.averagreview < 2%}-o empty checked {% endif %}"
                                                aria-hidden="true"></i>
                                            <i class="fa fa-star{% if doctor.averagreview < 3%}-o empty checked{% endif %}"
                                                aria-hidden="true"></i>
                                            <i class="fa fa-star{% if doctor.averagreview < 4%}-o empty checked{% endif %}"
                                                aria-hidden="true"></i>
                                            <i class="fa fa-star{% if doctor.averagreview < 5%}-o empty checked{% endif %}"
                                                aria-hidden="true"></i>
                                        </span>

                                        </span>
                                    </div>

I don't know why it show me the last rate not average did I make a mistake please if there is a solution write it and explain it

I think the problem is that you are using aggregate like annotate , that is you are defining a parameter where you don't need to define one. Try this:

reviews = Comments.objects.filter(doctor=self).aggregate(Avg('rating'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM