0

给定一个产品(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))
4

3 回答 3

0

您可以为此使用注释

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

这只是一个基本示例,可以根据您的需要进行扩展

于 2012-07-18T15:09:52.223 回答
0

尝试实现:

创建字典。它将 Product 作为键,将评级作为值。

循环遍历您的项目 ( Product.objects)

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

在该循环之外,按值对字典进行排序。(请参阅按值对 Python 字典进行排序

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

请注意,您必须处理具有 ex-aequo 评级的项目。确实,如果 10 个项目的分数相同,那么使用上述方法,您的 Top 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]
于 2012-07-18T14:56:25.167 回答
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]
于 2012-07-18T15:28:05.293 回答