`__unicode __ ()` addition does not work in the main survey application in the Django tutorial

I am working on a Django tutorial after installing the Django development source along with PostgreSQL from the source and everything else that is needed from the source. I am trying to do everything with python3 instead of python on Ubuntu 12.10.

Everything seemed to be going fine until I got to the part of the tutorial where we have to override __unicode__() to return a reasonable result when we query objects.all () from the table. This does not work at all. I decided to try __str__() and it worked!

But the tutorial explains that we should not redefine __str__() . So, what happened with my installation, what __unicode__() does not work, and __str__() does? Other methods from the tutorial work great.

+6
source share
1 answer

Lines are handled differently in Python 3 vs 2.

In 2, __str__() returned bytes, and __unicode__() returned characters. In 3, __str__() now returns characters, since strings are now initially unicode, and __unicode__() does not exist. If you really need the old 2 behavior for __str__() , I believe that now it is __bytes__() .

Short answer, stick with __str__() if you are using Python 3 and understand that Django tutorials explicitly state that they are written for 2.x, so there will be differences.

+8
source

All Articles