I’ve been working on a little side project in Django 1.3 and have run into a couple instances in the Django admin where I would get the error:
"Caught TypeError while rendering: coercing to Unicode: need string or buffer"
I did a little Bing-ing (my new default search engine — Google is too big, powerful and, dare I say, evil) and found the following solution from Ryan Brady.
For a Bing search of “django admin ‘Caught TypeError while rendering: coercing to Unicode: need string or buffer‘” there were only 4 results, but lucky for me, Ryan’s solved my problem.
Have you ever edited your models.py file and shortly after when trying to see the change list for a model in the admin site you received this error?
Caught an exception while rendering: coercing to Unicode: need string or buffer, NoneType found
I did today and it was one of those “bonehead†moments. You know, the one where you’ve been coding for a bit and for some reason make a simple error that leaves you thinking “I’ve done that exact same action a hundred times, why would I get an error this time?â€, when you realize you made a really simple mistake. The answer was quick and easy. It was not occuring on the new model form, only on the change list so I opened my models file and looked over the model in question and then realized my typo. I had forgot to add “return†to the __unicode__ method on my model. So if you get this error, make sure that you’re returning something and that its really unicode.
In my case, I actually had the correct “__unicode__(self)” method, but what I was returning wasn’t unicode, it was a user obect:
def __unicode__(self): return self.user
I changed it to the following and it worked:
def __unicode__(self): return self.user.username
Thank you, Ryan, for helping to solve my problem!
6 Responses to “Solving “Caught TypeError while rendering: coercing to Unicode: need string or buffer””