开发者

How to add attribute '_meta' to an object?

开发者 https://www.devze.com 2023-04-06 20:19 出处:网络
I\'m trying to add the django-voting app to my project. I do开发者_如何学运维n\'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 but

I'm trying to add the django-voting app to my project. I do开发者_如何学运维n'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'?


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

精彩评论

暂无评论...
验证码 换一张
取 消