diff options
| author | Tim Graham <timograham@gmail.com> | 2015-09-25 13:28:12 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-09-25 13:28:12 -0400 |
| commit | de99f558d806a2a1b30072ec95bc44d412d80dab (patch) | |
| tree | c4ce0c9de1c22aca12544278fac1e7c0898b8595 /docs/intro/tutorial02.txt | |
| parent | c42123adb166fd297116880a5322e4e17b11e33f (diff) | |
Fixed #25462 -- Removed Model.__unicode__() in favor of @python_2_unicode_compatible.
Diffstat (limited to 'docs/intro/tutorial02.txt')
| -rw-r--r-- | docs/intro/tutorial02.txt | 24 |
1 files changed, 5 insertions, 19 deletions
diff --git a/docs/intro/tutorial02.txt b/docs/intro/tutorial02.txt index de1f43708f..a92b757279 100644 --- a/docs/intro/tutorial02.txt +++ b/docs/intro/tutorial02.txt @@ -445,15 +445,18 @@ of this object. Let's fix that by editing the ``Question`` model (in the :filename: polls/models.py from django.db import models + from django.utils.encoding import python_2_unicode_compatible + @python_2_unicode_compatible # only if you need to support Python 2 class Question(models.Model): # ... - def __str__(self): # __unicode__ on Python 2 + def __str__(self): return self.question_text + @python_2_unicode_compatible # only if you need to support Python 2 class Choice(models.Model): # ... - def __str__(self): # __unicode__ on Python 2 + def __str__(self): return self.choice_text It's important to add :meth:`~django.db.models.Model.__str__` methods to your @@ -461,23 +464,6 @@ models, not only for your own convenience when dealing with the interactive prompt, but also because objects' representations are used throughout Django's automatically-generated admin. -.. admonition:: ``__str__`` or ``__unicode__``? - - On Python 3, it's easy, just use - :meth:`~django.db.models.Model.__str__`. - - On Python 2, you should define :meth:`~django.db.models.Model.__unicode__` - methods returning ``unicode`` values instead. Django models have a default - :meth:`~django.db.models.Model.__str__` method that calls - :meth:`~django.db.models.Model.__unicode__` and converts the result to a - UTF-8 bytestring. This means that ``unicode(p)`` will return a Unicode - string, and ``str(p)`` will return a bytestring, with characters encoded - as UTF-8. Python does the opposite: ``object`` has a ``__unicode__`` - method that calls ``__str__`` and interprets the result as an ASCII - bytestring. This difference can create confusion. - - If all of this is gibberish to you, just use Python 3. - Note these are normal Python methods. Let's add a custom method, just for demonstration: |
