Reputation: 2204
I'm getting TypeError: coercing to Unicode: need string or buffer, int found
This is my models.py:
class FollowingModel(models.Model):
user = models.ForeignKey(User)
person = models.IntegerField(max_length=20, blank=False)
def __unicode__(self):
return self.person
When I retrieve the values from the FollowingModel in my views like this
g = FollowingModel.objects.all()
g[0] -----> I'm getting that error
I tried changing the def __unicode__(self):
as
def __unicode__(self):
return str(self.person)
But no use, still I'm getting the same error. Could anyone guide me?
Thanks!
UPDATE
>>>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
Upvotes: 4
Views: 5694
Reputation: 1121196
The __unicode__
method should return just that, unicode:
def __unicode__(self):
return unicode(self.person)
Upvotes: 9