Django:使用Ratings应用程序的AttributeError [英] Django: AttributeError using ratings app

查看:101
本文介绍了Django:使用Ratings应用程序的AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我先前问题的扩展,但在解决之后这个问题我得到了不同的AttributeError.尝试在我的视图中执行以下操作时,该内容显示为'_RatingsDescriptor' object has no attribute 'cumulative_score':

This is somewhat an extension of my previous question, but after solving that problem I am getting a different AttributeError. This one reads '_RatingsDescriptor' object has no attribute 'cumulative_score' when trying to do something like this in my view:

def index(request):
    thing_list = Thing.ratings.cumulative_score()
    return render(request, 'index.html', {'thing_list':thing_list})

我的模特:

from ratings.models import Ratings

class Thing(models.Model):
    user = models.ForeignKey(User)
    ...
    ratings = Ratings()

使用 django-simple-ratings 应用程序.此链接引用了在该模块中定义.如何对多个对象使用累积分数?谢谢您的想法!

While using django-simple-ratings app. This link references where cumulative_score is defined in that module. How do I use cumulative score for multiple objects? Thank you for your ideas!

views.py:

def index(request):
    thing_list = Thing.objects.all()
    thing_list.ratings.cumulative_score()
    return render(request, 'index.html', {'thing_list':thing_list})

推荐答案

它是一个实例方法,因此必须首先从数据库中检索Thing.

Its an instance method, so you must first retrive Thing from the db.

t = Thing.objects.get(xxxx)
t.ratings.cumulative_score()

这篇关于Django:使用Ratings应用程序的AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆