From a8572b18a9a2bb2b73950ce078f7c3aae9e8e5af Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 30 Apr 2006 03:42:53 +0000 Subject: [PATCH] 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 --- docs/db-api.txt | 6 +++--- docs/model-api.txt | 18 +++++++++--------- docs/tutorial01.txt | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/db-api.txt b/docs/db-api.txt index 6b2911b3a3..8da6ebbac2 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -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 diff --git a/docs/model-api.txt b/docs/model-api.txt index ed1794f816..d5b88a2b06 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -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 diff --git a/docs/tutorial01.txt b/docs/tutorial01.txt index 8ce9f6539d..02d2795261 100644 --- a/docs/tutorial01.txt +++ b/docs/tutorial01.txt @@ -456,20 +456,20 @@ Once you're in the shell, explore the database API:: Wait a minute. ```` 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?]