gpt4 book ai didi

python - Django 评级错误 : "rating value must be a Rating instance, not ' 100"?

转载 作者:太空宇宙 更新时间:2023-11-03 18:34:13 25 4
gpt4 key购买 nike

我尝试通过 Django 评级应用程序评级来排序一组模型对象。我在我看来构建了一个 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 评级错误 : "rating value must be a Rating instance, not ' 100"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21889141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com