27

Simple Fix: coercing to Unicode: need string or buffer, NoneType found

Posted April 17th, 2010 in Django, General, Python and tagged , by ryan

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.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

27 Responses so far.

  1. Ankur says:

    Saved me hours of debugging. Thank you!

  2. rafar says:

    Thank you, you have helped me to figure out what I was doing wrong. My case is different, the error was “coercing to Unicode: need string or buffer, models.Model found”

    I am using an intermediate model for a ManyToMany (many-to-many) relationship. In my other models, the __unicode__ returns a CharField, but for this model, i was trying to return a ForeignKey which points to another model.

    The solution is to return a CharField of the model the ForeignKey points to; or, in my case, a concatenation of many CharFields.

  3. Fernando says:

    Thanks a lot

  4. Rafel Bennasar. says:

    Thanks! ^^

  5. goblin says:

    There also could be a case if you’re using `django-modeltranslation` app and have translated fields participating in model’s __unicode__() method.
    If you created some such objects, say, from shell, filling only original fields and leaving translated fields empty even for default language, then admin will throw you an exception like that one described above (‘NoneType found’) sooner or later.
    So you’ll need to do `./manage.py update_translation_fields` to fill required translation fields.

  6. Farid says:

    thank you. save me for a hours of debugging.

  7. Mitch says:

    Thanks Ryan, this error was driving me crazy!

  8. [...] 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 [...]

  9. Genius. I had like 8 if/elif statements in my __unicode__ function, but no final else to catch some odd case!

  10. Timmy says:

    Ugh! Thank you – just suffered this for 10 minutes too long

  11. Dean says:

    You saved me hours.
    You inspired me to write blog entries of bonehead mistakes I make for others to not spend hours debugging it.

    Thank you Thank you Thank you.
    :)

  12. Ben says:

    Life saver,

    I will reiterate what everyone else said.

    Thank you, Thank you, Thank you.

  13. seb says:

    Haha, i printed only what i wanted to return ….

  14. fokion says:

    thanks mate!
    i’m new to Python and Django, and you sure saved me from some struggling! ;p
    cheers!

  15. chang min says:

    THANK YOU!!!! SOOOOOOOOOOOOOO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    You are so genius!!!
    How can I express my gratitude fully for this post??!!!!
    2 f—— hours went me crazy!!!!!!
    and found this article!!!
    and solved out my problem!!
    THANKS SO MUCH A LOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  16. Gordio says:

    Or
    def __unicode__(self):
    return self.date

    need:
    def __unicode__(self):
    return “%s” % self.date

  17. ryan says:

    Gordio,

    Yes – self.date in this case, if coming from a model with a DateField or DateTimeField will return an object of the datetime.datetime type, not a string/unicode.

    - r

  18. Luis says:

    OH!, Finally, yes, my problem was also with a model. I was returning a foreign key. I am new and I need to figure aout how to return it as a char. I just removed and began to work. Excellent. Thank you.

  19. estani says:

    Thanks, you help me sort this out in 10s. So I can spend another 20s just to thank you :-)

    Completely different problem here, but pointed in the right direction.
    (basically I had a @classmethod though the method was designed for @statimethod, I got the class instead of the string I passed…)

  20. Tomasz jadczuk says:

    thank you! two day of madness, lot of coffees, sites, communities, books and you save me from myself.
    Thank You! You are great!

  21. Antonio Mello says:

    Thanks Bro. Exactly the same stupid thing! :)

  22. Caio says:

    I was doing the same thing. Since i start coding, two years ago, i dreamed about UTF and ASCII, but I have got it right. I guess its time to read.

    Anyway I got this tips which helps keep me going.

    http://mitchfournier.com/2011/04/21/solving-caught-typeerror-while-rendering-coercing-to-unicode-need-string-or-buffer/

    Soon more news.

  23. Margon says:

    Whoa! Thank you so much!

    My case was similar to Rafar’s one. Many to many relationship modeling and trying to return the self.id by itselfs. Just added the str wrapper to return a actual string.

    Many thanks

  24. Olga says:

    Thanks a lot!! It saved me a lot of time and stress :)

  25. Carlos Ramos says:

    Awesome, to think that it was only a return statement!
    Thanks :D .

  26. Bahtiyor says:

    Thank you very much :-) )

  27. As noted in the other replies, you’re probably going to want to supply an error handler to the encode method. Using ‘replace’ as the error handler is simple, but will mangle your text if it contains characters that cannot be represented in ASCII.

Leave a Reply





FireStats icon Powered by FireStats