Django:为具有多个评分标准的评论网站生成评分平均值

时间:2019-11-11 02:43:10

标签: python django model-view-controller review rating-system

正在使用Python(3.8)和Django开发一个项目,试图实现具有多个条件的审阅系统。

注意:我已经搜索了一段时间,但没有阅读任何能专门解决我的问题的东西。

我有3种型号:

from django.db import models
from django.conf import settings
from django.urls import reverse


# Create your models here.
class Board(models.Model):
    name = models.CharField(max_length=30, unique=True)
    description = models.CharField(max_length=100)

    def __str__(self):
        return self.name

    def get_reviews_count(self):
        return Review.objects.filter(company__board=self) .count()

    def get_last_review(self):
        return Review.objects.filter(company__board=self) .order_by('created_at') .first()

class Company(models.Model):
    name = models.CharField(max_length=255, unique=True)
    #bio = models.TextField(max_length=4000)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board, on_delete = models.CASCADE, related_name='companies')
    starter = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='companies',
    )
    views = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('company_detail', args=[str(self.id)])

    def get_recent_reviews(self):
        return self.reviews.order_by('-created_at')

    def get_last_ten_reviews(self):
        return self.reviews.order_by('-created_at')[:10]

class Review(models.Model):
    RATING_CHOICES = (
        ('1', '1'),
        ('2', '2'),
        ('3', '3'),
        ('4', '4'),
        ('5', '5'),
    )
    STAY = (
        ('less than 6 months', 'less than 6 months'),
        ('6 months', '6 months'),
        ('10 months', '10 months'),
        ('12 months', '12 months'),
        ('More than 1 year', 'More than 1 year'),
    )
    YES_NO = (
        ('Yes', 'Yes'),
        ('No', 'No'),
    )
    SECURITY = (
        ('100%', '100%'),
        ('75%', '75%'),
        ('50%', '50%'),
        ('25%', '25%'),
        ('0%', '0%'),
        ('Still waiting', 'Still waiting'),
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE, related_name='reviews')
    updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE, null=True, related_name='+')
    company = models.ForeignKey(Company, on_delete = models.CASCADE, related_name='reviews')
    address = models.CharField(max_length=200, blank=False, default="")
    length_of_stay = models.CharField(max_length=20, choices=STAY, blank=False)
    #Apartment Condition
    move_in_condition = models.CharField(max_length=5,choices=RATING_CHOICES)
    #Landlord Interaction
    treatment = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="3")
    response_speed = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="3")
    maintenance_quality = models.CharField(max_length=5, choices=RATING_CHOICES, blank=False, default ="3")

    security_deposit_returned = models.CharField(max_length=5, choices=SECURITY, blank=False, default ="1")
    #put text "ignore if still waiting"
    is_this_a_fair_amount = models.CharField(max_length=5, choices=YES_NO, blank=False, default="1")
    would_you_recommend = models.CharField(max_length=5, choices=YES_NO, blank=False, default="1")
    additional_comments = models.TextField(max_length=4000)

    def __str__(self):
        return self.address

Views.py

def board_companies(request, pk):
    board = get_object_or_404(Board, pk=pk)
    companies = board.companies.order_by('-last_updated') .annotate(replies=Count('reviews'))
    return render(request, 'companies.html', {'board': board, 'companies': companies})

def company_reviews(request, pk, company_pk):
    company = get_object_or_404(Company, board__pk=pk, pk=company_pk)
    company.views += 1
    company.save()
    return render(request, 'company_reviews.html', {'company': company})

用户发布有关公司的评论,并且在评论表单中有很多条件。

我想对一些标准进行平均,以获取评论帖子的总体评分,该评分将与每个评论帖子一起显示。

然后对公司的所有总体评分进行平均,并在公司的评论页和公司列表页上显示。

然后单独找到每个单独标准的总平均值,并将其显示在总体评分(治疗平均值,response_speed平均值等)下方的公司评论页上

提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用F表达式并将聚合与注释结合起来

# Annotate with average of multiple rating columns, 
# then aggregate all overall ratings and each individual rating of all reviews
Company.objects.annotate(overall_rating = Avg(
    F('move_in_condition') + 
    F('treatment') + 
    F('response_speed') +
    F('maintenance_quality') / 4
)).aggregate(
    Avg('overall_rating'), 
    Avg('move_in_condition'),
    Avg('treatment'),
    Avg('response_speed'),
    Avg('maintenance_quality')
)

这整个语句执行您指定的所有三件事。 您可能需要在Avg函数中指定output_field参数,以防出现错误

Havent'对此进行了测试,但是我直接基于docs: https://docs.djangoproject.com/en/2.2/ref/models/expressions/#f-expressions https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#aggregating-annotations