如何将平均评分显示为星级?



我正在开发我的web应用程序,我想向医生显示平均评分,但是当我尝试avg并在我的模型中创建一个函数时,它只显示了我最后的评分,而不是平均值

这是我的模型

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)
这是我的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>

我不知道为什么它显示我最后的利率不是平均我犯了一个错误,如果有一个解决方案,请写下来并解释它

我认为问题是你使用aggregateannotate,也就是说你正在定义一个参数,你不需要定义一个。试试这个:

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

相关内容

  • 没有找到相关文章

最新更新