gpt4 book ai didi

python - django - 类型错误 : coercing to Unicode

转载 作者:太空狗 更新时间:2023-10-30 02:05:28 27 4
gpt4 key购买 nike

我收到TypeError: coercing to Unicode: need string or buffer, int found

这是我的模型.py:

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

def __unicode__(self):
return self.person

当我像这样在我的 View 中检索 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

最佳答案

__unicode__ method should return just that, unicode :

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

关于python - django - 类型错误 : coercing to Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11261986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com