diff options
| author | Tim Graham <timograham@gmail.com> | 2015-09-25 13:28:12 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-09-25 15:36:05 -0400 |
| commit | 260e9f15fe05226188a63ef8e30fa069e48f2660 (patch) | |
| tree | d90a63d2685c828133a82ff97ca9eca6fef90bb7 /docs/intro | |
| parent | ffcf81969b50e6746061e5a687363faa667959f3 (diff) | |
[1.9.x] Fixed #25462 -- Removed Model.__unicode__() in favor of @python_2_unicode_compatible.
Backport of de99f558d806a2a1b30072ec95bc44d412d80dab from master
Diffstat (limited to 'docs/intro')
| -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: |
