1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

magic-removal: Replaced __repr__() with __str__() in docs

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2795 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-04-30 03:42:53 +00:00
parent 5917d90e30
commit a8572b18a9
3 changed files with 17 additions and 17 deletions

View File

@ -15,14 +15,14 @@ a weblog application::
name = models.CharField(maxlength=100)
tagline = models.TextField()
def __repr__(self):
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(maxlength=50)
email = models.URLField()
class __repr__(self):
class __str__(self):
return self.name
class Entry(models.Model):
@ -32,7 +32,7 @@ a weblog application::
pub_date = models.DateTimeField()
authors = models.ManyToManyField(Author)
def __repr__(self):
def __str__(self):
return self.headline
Creating objects

View File

@ -966,9 +966,9 @@ Here's a list of all possible ``Admin`` options. No options are required. Adding
``short_description`` function attribute, for use as the header for
the field.
* Use the string ``"__repr__"`` to output the representation of the
object, according to your model's ``__repr__()`` function. If you
don't define ``list_display``, Django will use the ``__repr__`` by
* Use the string ``"__str__"`` to output the representation of the
object, according to your model's ``__str__()`` function. If you
don't define ``list_display``, Django will use the ``__str__`` by
default.
See the example below.
@ -1131,16 +1131,16 @@ See `Giving models custom methods`_ for a full example.
A few object methods have special meaning:
``__repr__``
Django uses ``repr(obj)`` in a number of places, most notably as the value
``__str__``
Django uses ``str(obj)`` in a number of places, most notably as the value
inserted into a template when it displays an object. Thus, you should always
return a nice, human-readable string for the object's ``__repr__``.
return a nice, human-readable string for the object's ``__str__``.
Although defining ``__repr__()`` isn't required, it's strongly encouraged.
Although defining ``__str__()`` isn't required, it's strongly encouraged.
See `Adding repr`_ for a full example.
See `Adding str`_ for a full example.
.. _Adding repr: http://www.djangoproject.com/documentation/models/repr/
.. _Adding str: http://www.djangoproject.com/documentation/models/repr/
``get_absolute_url``
Define a ``get_absolute_url`` method to tell Django how to calculate the

View File

@ -456,20 +456,20 @@ Once you're in the shell, explore the database API::
Wait a minute. ``<Poll object>`` is, utterly, an unhelpful representation of
this object. Let's fix that by editing the polls model
(in the ``polls/models.py`` file) and adding a ``__repr__()`` method to
(in the ``polls/models.py`` file) and adding a ``__str__()`` method to
both ``Poll`` and ``Choice``::
class Poll(models.Model):
# ...
def __repr__(self):
def __str__(self):
return self.question
class Choice(models.Model):
# ...
def __repr__(self):
def __str__(self):
return self.choice
It's important to add ``__repr__()`` methods to your models, not only for your
It's important to add ``__str__()`` methods to your models, not only for your
own sanity when dealing with the interactive prompt, but also because objects'
representations are used throughout Django's automatically-generated admin.
@ -491,7 +491,7 @@ Let's jump back into the Python interactive shell by running
>>> from mysite.polls.models import Poll, Choice
# Make sure our __repr__() addition worked.
# Make sure our __str__() addition worked.
>>> Poll.objects.all()
[What's up?]