我正在尝试创建一个显示产品列表的 Django 站点,并根据他们收到的总票数对它们进行排序。 'Product' 是一个类,而 'Vote' 是另一个类,有一个称为 score 的字段,它可以是 1、0 或 -1。我希望根据与每个特定产品相关的投票得分的总和对产品进行排序。
这是我的models.py:
class Product(models.Model):
Content = models.TextField()
creation_date = models.DateTimeField( default=datetime.now )
total_votes = models.IntegerField( default=0 )
def __unicode__(self):
return self.content
class Vote(models.Model):
voter = models.ForeignKey( User )
product = models.ForeignKey( Product )
score = models.IntegerField( default=0, choices=VOTE_SCORE_CHOICES)
def __unicode__(self):
return '%s - %s : %s' % (self.product.id, self.voter, self.score)
这是我的 views.py:
def show_products( request):
product_list = Product.objects.all()
# set the field 'total_votes' in prduct to the sum of votes for each sentence
for p in product_list:
try:
v = Vote.objects.filter( product = p ).aggregate(Sum('score'))['score__sum']
except IndexError:
v = 0
p.total_votes = v
p.save()
# get the product that has the highest vote score
try:
top_product = product_list.order_by('-total_votes')[0].content
except IndexError:
top_product = 'no product'
# reorder product_list according to total_votes
product_list = product_list.order_by('total_votes')
return render_to_response('product/product_detail.html',
{'product_list': product_list,
'top_produce': top_product,}, context_instance=RequestContext(request))
所以你看到我得到了与每个产品相关的投票得分的总和,然后将此数字分配给每个产品的“total_votes”字段,然后根据“total_votes”重新排序产品列表。
然而,结果并不如预期,产品与投票分数分开排序。有人能告诉我这里的代码有什么问题吗?而且,这是解决这个问题的正确方法吗?
谢谢
最佳答案
这似乎是一种不必要的复杂方法。您分别为每个产品汇总投票,将结果保存在产品中,然后按这些投票总和进行排序。
取而代之的是,您似乎应该一口气完成所有操作:
product_list = Product.objects.annotate(total_votes=Sum('vote__score')).order_by('total_votes')
(我假设您对
sentence
和 b
的初始引用分别是指 product
和 p
- 否则您需要提供有关这些实际内容的更多详细信息。)
关于django - 按 Django 中的总和排序查询,但未按预期获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278796/