我试图通过使用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)

最佳答案

尝试重命名额外的属性“评级”

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')

关于python - Django-ratings错误:“评级值必须是评级实例,而不是'100”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21889141/

10-12 16:35