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.
__unicode__needs to return aunicodeobject.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 callunicodeon them: