如何获得一个类别中排名靠前的产品?

时间:2012-07-18 14:43:04

标签: django view methods models

鉴于产品(product_name是视图中的参数),我试图将该类别中的5个排名靠前的产品(由方法“get_avg_rating”定义)作为一个列表返回,我可以在模板。关于如何做到这一点的任何建议?

class Productbackup(models.Model):
    website = models.CharField('Product name', max_length = 200)
    url_friendly = models.CharField('URL friendly', max_length = 200)
    website_url = models.URLField('Product URL')
    description= models.CharField('Description', max_length = 2000)
    category = models.ForeignKey(Categories)
    #category = models.ManyToManyField(Categories)
    image_hero = models.URLField('Hero image url')
    image_second = models.URLField('Second image url')
    image_third = models.URLField('Third image url')
    created_on = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    def __unicode__(self):
        return self.website

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

    def get_num_reviews(self):
        reviews = Reviewbackup.objects.filter(product=self)
        count = len(reviews)
        return count

RATING_OPTIONS = (
    (1, '1'),
    (2, '2'),
    (3, '3'),
    (4, '4'),
    (5, '5'),
                (6, '6'),
    (7, '7'),
    (8, '8'),
    (9, '9'),
    (10, '10'),
)
class Reviewbackup(models.Model):
    review = models.CharField('Review', max_length = 2000)
    created_on = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    user = models.CharField('Username', max_length =  200)
    rating = models.IntegerField(max_length=2, choices=RATING_OPTIONS)
    product = models.ForeignKey(Productbackup)
    def __unicode__(self):
        return self.review

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

def view_reviews(request, product_name):
    product = get_object_or_404(Productbackup, url_friendly=product_name)
    product_id = product.id
    #get reviews for the this product
    reviews = Reviewbackup.objects.filter(product_id=product_id).order_by("-created_on")
    #similar products in category comparison
    prod_category = Productbackup.objects.filter(category=product.category)
    #top_ranked = Productbackup.objects.order_by('get_avg_rating')[0:5]
    #recently added
    recent_added = Productbackup.objects.order_by('-created_on')[0:5]
    return render_to_response('reserve/templates/view_reviews.html', {'prod_category': prod_category, 'product':product, 'reviews':reviews, 'recent_added':recent_added},
    context_instance=RequestContext(request))

3 个答案:

答案 0 :(得分:0)

尝试实现:

创建字典。它将产品作为密钥,评级作为值。

循环浏览您的商品(Product.objects

在该循环中,将每个项目放在字典中

在该循环之外,按值对字典进行排序。 (见Sort a Python dictionary by value

获取字典的最后一项(dictionary[-5])。

请注意,您必须处理具有前额定值的项目。实际上,如果10个项目具有相同的分数,那么您的前5个并不意味着使用上述方法。

这将产生类似于此的代码:

items_with_rating = {}
for product in Product.objects:
    items_with_rating[product] = product.get_avg_rating()

items_sorted_by_rating = sorted(items_with_rating.iteritems(), key=operator.itemgetter(1))

top5_items = items_sorted_by_rating[-5]

答案 1 :(得分:0)

您可以{/ 3}}使用

from django.db.models import Sum, Avg

def get_top_products(amount=5):
    try:
         Productbackup.objects.annotate(review_amount=Sum("reviewbackup__product")).\
                               order_by("review_amount")[:amount]
    except Productbackup.DoesNotExist:
        return None

这只是一个基本的例子,可以扩展到您的需求

答案 2 :(得分:0)

也许这样的事情可以奏效?

products = [[prod, prod.get_avg_rating()] for prod in Productbackup.objects()]

top_5_items = sorted(products, key=lambda x: x[1], reverse=True)[:5]