Fixed #25508 -- Modified QuerySet.__repr__() to disambiguate it from a list.

This commit is contained in:
Tim Graham 2015-10-05 19:07:34 -04:00
parent 3543fec3b7
commit e0837f2cb1
20 changed files with 185 additions and 184 deletions

View file

@ -73,7 +73,7 @@ necessary:
# No reporters are in the system yet.
>>> Reporter.objects.all()
[]
<QuerySet []>
# Create a new Reporter.
>>> r = Reporter(full_name='John Smith')
@ -87,7 +87,7 @@ necessary:
# Now the new reporter is in the database.
>>> Reporter.objects.all()
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
# Fields are represented as attributes on the Python object.
>>> r.full_name
@ -113,7 +113,7 @@ necessary:
# Now the article is in the database.
>>> Article.objects.all()
[<Article: Django is cool>]
<QuerySet [<Article: Django is cool>]>
# Article objects get API access to related Reporter objects.
>>> r = a.reporter
@ -122,13 +122,13 @@ necessary:
# And vice versa: Reporter objects get API access to Article objects.
>>> r.article_set.all()
[<Article: Django is cool>]
<QuerySet [<Article: Django is cool>]>
# The API follows relationships as far as you need, performing efficient
# JOINs for you behind the scenes.
# This finds all articles by a reporter whose name starts with "John".
>>> Article.objects.filter(reporter__full_name__startswith='John')
[<Article: Django is cool>]
<QuerySet [<Article: Django is cool>]>
# Change an object by altering its attributes and calling save().
>>> r.full_name = 'Billy Goat'

View file

@ -401,7 +401,7 @@ Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
# No questions are in the system yet.
>>> Question.objects.all()
[]
<QuerySet []>
# Create a new Question.
# Support for time zones is enabled in the default settings file, so
@ -432,8 +432,7 @@ Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
# objects.all() displays all the questions in the database.
>>> Question.objects.all()
[<Question: Question object>]
<QuerySet [<Question: Question object>]>
Wait a minute. ``<Question: Question object>`` is, utterly, an unhelpful representation
of this object. Let's fix that by editing the ``Question`` model (in the
@ -494,14 +493,14 @@ Save these changes and start a new Python interactive shell by running
# Make sure our __str__() addition worked.
>>> Question.objects.all()
[<Question: What's up?>]
<QuerySet [<Question: What's up?>]>
# Django provides a rich database lookup API that's entirely driven by
# keyword arguments.
>>> Question.objects.filter(id=1)
[<Question: What's up?>]
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(question_text__startswith='What')
[<Question: What's up?>]
<QuerySet [<Question: What's up?>]>
# Get the question that was published this year.
>>> from django.utils import timezone
@ -535,7 +534,7 @@ Save these changes and start a new Python interactive shell by running
# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
[]
<QuerySet []>
# Create three choices.
>>> q.choice_set.create(choice_text='Not much', votes=0)
@ -550,7 +549,7 @@ Save these changes and start a new Python interactive shell by running
# And vice versa: Question objects get access to Choice objects.
>>> q.choice_set.all()
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
>>> q.choice_set.count()
3
@ -560,7 +559,7 @@ Save these changes and start a new Python interactive shell by running
# Find all Choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
>>> Choice.objects.filter(question__pub_date__year=current_year)
[<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
# Let's delete one of the choices. Use delete() for that.
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking')

View file

@ -383,7 +383,7 @@ With that ready, we can ask the client to do some work for us::
>>> # If the following doesn't work, you probably omitted the call to
>>> # setup_test_environment() described above
>>> response.context['latest_question_list']
[<Question: Who is your favorite Beatle?>]
<QuerySet [<Question: Who is your favorite Beatle?>]>
Improving our view
------------------