17

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]

17 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

Leave a Reply





FireStats icon Powered by FireStats