-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #20224 -- Update docs examples which mention __unicode__ #1330
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -124,13 +124,16 @@ Model style | ||
first_name = models.CharField(max_length=20) | first_name = models.CharField(max_length=20) | ||
last_name = models.CharField(max_length=40) | last_name = models.CharField(max_length=40) | ||
|
|
||
* If you define a ``__str__`` method (previously ``__unicode__`` before Python 3 | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment and the one above are worded differently. It would be nice to have consistency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marc, I'm sorry but I'm not getting your idea here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost every place in the documentation we say " |
|||
was supported), decorate the model class with | |||
:func:`~django.utils.encoding.python_2_unicode_compatible`. | |||
|
|||
* The order of model inner classes and standard methods should be as | * The order of model inner classes and standard methods should be as | ||
follows (noting that these are not all required): | follows (noting that these are not all required): | ||
|
|
||
* All database fields | * All database fields | ||
* Custom manager attributes | * Custom manager attributes | ||
* ``class Meta`` | * ``class Meta`` | ||
* ``def __unicode__()`` | |||
* ``def __str__()`` | * ``def __str__()`` | ||
* ``def save()`` | * ``def save()`` | ||
* ``def get_absolute_url()`` | * ``def get_absolute_url()`` | ||
|
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -430,6 +430,12 @@ Other model instance methods | ||
|
|
||
A few object methods have special purposes. | A few object methods have special purposes. | ||
|
|
||
.. note:: | |||
On Python 3, as all strings are natively considered Unicode, only use the | |||
``__str__()`` method, which entirely obsoletes the ``__unicode__()`` method. | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd replace
with
|
|||
If you'd like compatibility with Python 2, you can decorate your model class | |||
with :func:`~django.utils.encoding.python_2_unicode_compatible`. | |||
|
|||
``__unicode__`` | ``__unicode__`` | ||
--------------- | --------------- | ||
|
|
||
|
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -15,6 +15,7 @@ To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`. | ||
last_name = models.CharField(max_length=30) | last_name = models.CharField(max_length=30) | ||
email = models.EmailField() | email = models.EmailField() | ||
|
|
||
# On Python 3: def __str__(self): | |||
def __unicode__(self): | def __unicode__(self): | ||
return u"%s %s" % (self.first_name, self.last_name) | return u"%s %s" % (self.first_name, self.last_name) | ||
|
|
||
|
@@ -23,6 +24,7 @@ To define a many-to-one relationship, use :class:`~django.db.models.ForeignKey`. | ||
pub_date = models.DateField() | pub_date = models.DateField() | ||
reporter = models.ForeignKey(Reporter) | reporter = models.ForeignKey(Reporter) | ||
|
|
||
# On Python 3: def __str__(self): | |||
def __unicode__(self): | def __unicode__(self): | ||
return self.headline | return self.headline | ||
|
|
||
|
@@ -56,9 +58,9 @@ Article objects have access to their related Reporter objects:: | ||
|
|
||
>>> r = a.reporter | >>> r = a.reporter | ||
|
|
||
These are strings instead of unicode strings because that's what was used in | On Python 2, these are strings instead of unicode strings because that's what | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add: strings of type |
|||
the creation of this reporter (and we haven't refreshed the data from the | was used in the creation of this reporter (and we haven't refreshed the data | ||
database, which always returns unicode strings):: | from the database, which always returns unicode strings):: | ||
|
|
||
>>> r.first_name, r.last_name | >>> r.first_name, r.last_name | ||
('John', 'Smith') | ('John', 'Smith') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add () on str for consistency with "unicode()"