Reputation: 3
I bit confuse with __unicode__()
in django. I'm still a newbie.
I have this code:
models.py
class Order(models.Model):
order_num = models.IntegerField(verbose_name="OR Number")
order_date = models.DateTimeField(auto_now_add=True,verbose_name="OR Date")
customer = models.ForeignKey(User)
def __unicode__(self):
return self.order_num
I register it on the admin side. When I tried to add order it has an error:
TypeError at /admin/store/order/add/
coercing to Unicode: need string or buffer, int found
What will I declare on the method __unicode__(self)
?
It is clear I don't have string on my fields. How can I declare buffer?
or
Anyone has another answer.. please help.. Thanks.
Upvotes: 0
Views: 589
Reputation: 176780
__unicode__
needs to return a unicode
object.
The error you see is because only string or buffer-protocol objects can be automatically converted (it's still better to explicitly convert them).
For built-in Python types, or any user-defined class that has its own __unicode__
method, you can just call unicode
on them:
def __unicode__(self):
return unicode(self.order_num)
Upvotes: 4
Reputation: 10119
You should declare your __unicode__
function like this:
def __unicode__(self):
return u'%s' % self.order_num
More info in Other model instance methods
Upvotes: 0