我试图通过使用 Django-ratings 应用程序评级来订购一组模型对象。我已经在我的视图中构建了一个 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)