django - TypeError:强制转换为Unicode

时间:2012-06-29 12:42:23

标签: python django

我收到 TypeError:强制转换为Unicode:需要字符串或缓冲区,找到int

这是我的models.py:

class FollowingModel(models.Model):
    user = models.ForeignKey(User)
    person = models.IntegerField(max_length=20, blank=False)

    def __unicode__(self):
            return self.person

当我在我的视图中检索 FollowingModel 中的值时

g = FollowingModel.objects.all()
g[0] -----> I'm getting that error

我尝试将def __unicode__(self):更改为

def __unicode__(self):
    return str(self.person)

但没有用,我仍然得到同样的错误。有谁能指导我?

谢谢!

更新

>>>g = FollowingModel.objects.all()
>>>g

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 72, in __repr__
return repr(data)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 370, in __repr__
u = unicode(self)
TypeError: coercing to Unicode: need string or buffer, int found

1 个答案:

答案 0 :(得分:9)

__unicode__ method should return just that, unicode

def __unicode__(self):
    return unicode(self.person)