我试图通过使用Django-ratings app进行评级来订购一组模型对象。我在我的视图中构建了一个order_by函数,但是当我进入评级字段时我得到了这个错误:“评级值必须是评级实例,而不是'100”。任何想法都有错误/或错误意味着什么?
谢谢!
模板:
<a href="/mylist/?order_by=rating">Order by Rating</a>
查看:
def Mylist(request):
order_by = request.GET.get('order_by', 'created_date')
mylist = Mymodel.objects.filter(user=request.user).order_by(order_by)
if order_by == "rating":
qs = Mymodel.objects.filter(user=request.user).extra(select={
'rating': '((100/%s*rating_score/(rating_votes+%s))+100)/2' % (Mymodel.rating.range, Mymodel.rating.weight)
})
mylist = qs.order_by('-rating')
context = {'mylist ': mylist }
return render_to_response('mylist.html', context, context_instance=RequestContext(request))
型号:
class Mymodel(models.Model):
user = models.ForeignKey(User)
created_date = models.DateField(auto_now_add=True)
type = models.CharField(max_length=50)
description = models.CharField(max_length=140, blank=True)
rating = RatingField(range=5)
答案 0 :(得分:1)
尝试重命名额外属性'rating'
queryset = queryset.extra(select={
'myrating': '((100/%s*rating_score/(rating_votes+%s))+100)/2' % (Mymodel.rating.range, Mymodel.rating.weight)
})
queryset = queryset.order_by('-myrating')