如何将属性“_meta"添加到对象? [英] How to add attribute '_meta' to an object?

查看:25
本文介绍了如何将属性“_meta"添加到对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 django-voting 应用程序添加到我的项目中.我不知道如何在我的模板中使用它,所以我添加了一个新的模板标签,用于在用户单击按钮时投票赞成或反对.不知道有没有好的表格来做.

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 打印:

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

如何将 _meta 属性添加到我的对象位置"?

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

推荐答案

问题是这里的 obj 实际上并不是一个对象,而是一个包含一个元素的查询集.您应该使用 get 而不是 filter,因为 get 实际上返回一个模型实例.

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)

这篇关于如何将属性“_meta"添加到对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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