Ticket #4814: tutorial01.diff
File tutorial01.diff, 1.6 KB (added by , 17 years ago) |
---|
-
tutorial01.txt
494 494 495 495 .. admonition:: Why ``__unicode__()`` and not ``__str__()``? 496 496 497 498 497 If you're familiar with Python, you might be in the habit of adding 498 ``__str__()`` methods to your classes, not ``__unicode__()`` methods. 499 499 We use ``__unicode__()`` here because Django models deal with Unicode by 500 500 default. All data stored in your database is converted to Unicode when it's 501 501 returned. 502 502 503 Django models have a default ``__str__()`` method that calls ``__unicode__()`` 504 and converts the result to a UTF-8 bytestring. This means that ``unicode(p)`` 505 will return a Unicode string, and ``str(p)`` will return a normal string, 506 503 Django models have a default ``__str__()`` method that calls 504 ``__unicode__()`` and converts the result to a UTF-8 bytestring. This means 505 that ``unicode(p)`` will return a Unicode string, and ``str(p)`` will return 506 a normal string, with characters encoded as UTF-8. 507 507 508 509 508 If all of this is jibberish to you, just remember to add ``__unicode__()`` 509 methods to your models. With any luck, things should Just Work for you. 510 510 511 511 Note these are normal Python methods. Let's add a custom method, just for 512 512 demonstration::