How to add attribute '_meta' to an object?

99封情书 提交于 2019-12-02 02:22:01

问题


I'm trying to add the django-voting app to my project. I don't know how to use it in my templates, so I'm adding a new template tags for voting up or down when an user click in buttons. I don't know if there's a well form to do it.

My problem is with these kind of line in the template tag:

obj = Place.objects.filter(id=object_id)
Vote.objects.record_vote(obj, self.user, +1)

django print:

Caught AttributeError while rendering: 'Place' object has no attribute '_meta'

How I can add the attribute _meta my object 'Place'?


回答1:


The problem is that obj here is not actually an object, but a queryset with one element. You should use get instead of filter, as get actually returns a model instance.

obj = Place.objects.get(id=object_id)


来源:https://stackoverflow.com/questions/7532025/how-to-add-attribute-meta-to-an-object

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