Django: AttributeError using ratings app

夙愿已清 提交于 2019-12-25 09:35:10

问题


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

My model:

from ratings.models import Ratings

class Thing(models.Model):
    user = models.ForeignKey(User)
    ...
    ratings = 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!


EDIT: views.py:

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

回答1:


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

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



来源:https://stackoverflow.com/questions/14227478/django-attributeerror-using-ratings-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!