如何在django中获得不同模型的平均值?

时间:2012-06-29 02:11:47

标签: django templates view model average

我试图获得给定类别中所有产品的每种产品的评级平均值(在评论模型中找到)。我这样做有困难。相关的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 %}

2 个答案:

答案 0 :(得分:1)

我将在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 %}

注意:在代码中传递'allproducts'而不是'product'。因此,相应地更新视图代码。在您的代码{% for product in product %}中使用相同的变量名称,这是不正确的。

答案 1 :(得分:0)

执行此操作的最有效的方法(无迭代)是将aggregationsAvg()方法一起使用,

    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 %}