0

我正在尝试获取给定类别中所有产品的每个产品的平均评分(在评论模型中找到)。我很难做到这一点。相关的 urlconf、视图和模板如下:

url(r'^category/(?P<category_id>\d+)/$', 'view_product_category'),

class Productbackup(models.Model):
    website = models.CharField('Product name', max_length = 200)
    website_url = models.URLField('Product URL')
    category = models.ForeignKey(Categories)
    def __unicode__(self):
        return self.website

class ProductbackupForm(ModelForm):
    class Meta:
        model = Productbackup

def get_avg_rating(self):
    reviews = Reviewbackup.objects.filter(product=self)
    count = len(reviews)
    sum = 0
    for rvw in reviews:
        sum += rvw.rating
    return (sum/count)

class Reviewbackup(models.Model):
    review = models.CharField('Review', max_length = 2000)
    rating = models.IntegerField(max_length=200)
    product = models.ForeignKey(Product)
    def __unicode__(self):
        return self.review

class Categories(models.Model):
    category = models.CharField('Category_second', max_length = 200)
    def __unicode__(self):
        return unicode(self.category)

def view_product_category(request, category_id):
    allproduct = Productbackup.objects.filter(category_id=category_id)
    #get average for each product within a category
    return render_to_response('reserve/templates/view_category.html', {'allproduct':allproduct},
    context_instance=RequestContext(request))

{% for product in allproduct %}
    <a href = "/products/{{ product.id}}/reviews"> {{ product.website }}</a>: <a href = "{{ product.website_url }}"> Direct link</a><br>
    #average rating for that product
{{ product.get_avg_rating }}    
{% endfor %}
4

2 回答 2

2

我将在Product模型上添加方法以获得平均评分。

class Productbackup(models.Model):
    website = models.CharField('Product name', max_length = 200)
    website_url = models.URLField('Product URL')
    category = models.ForeignKey(Categories)
    def __unicode__(self):
        return self.website

    def get_avg_rating(self):
        reviews = Reviewbackup.objects.filter(product=self)
        count = len(reviews)
        sum = 0
        for rvw in reviews:
            sum += rvw.rating
        return (sum/count)

在模板中:

{% for product in allproducts %}
    <a href = "/products/{{ product.id}}/reviews"> {{ product.website }}</a>: <a href = "{{ product.website_url }}"> Direct link</a><br>
    #average rating for that product    
    {{ product.get_avg_rating }}
{% endfor %}

注意:在您的代码中传递“所有产品”而不是“产品”。所以相应地更新视图代码。在您的代码{% for product in product %}中使用相同的变量名,这是不正确的。

于 2012-06-29T04:33:02.163 回答
0

非常有效(无需迭代)的方法是使用方法聚合Avg()

    class Productbackup(models.Model):
        website = models.CharField('Product name', max_length = 200)
        website_url = models.URLField('Product URL')
        category = models.ForeignKey(Categories)
        def __unicode__(self):
            return self.website

        def get_avg_rating(self):
            reviews = Reviewbackup.objects.filter(product=self).aggregate(rating_avg=Avg('rating'))
            return (reviews['rating_avg'])

在模板中:

    {% for product in allproducts %}
        <a href = "/products/{{ product.id}}/reviews"> {{ product.website }}</a>: <a href = "{{ product.website_url }}"> Direct link</a><br>
        #average rating for that product    
        {{ product.get_avg_rating }}
    {% endfor %}
于 2020-10-16T13:43:30.787 回答