Commit e0837f2c authored by Tim Graham's avatar Tim Graham
Browse files

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

parent 3543fec3
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -234,7 +234,7 @@ class QuerySet(object):
        data = list(self[:REPR_OUTPUT_SIZE + 1])
        if len(data) > REPR_OUTPUT_SIZE:
            data[-1] = "...(remaining elements truncated)..."
        return repr(data)
        return '<QuerySet %r>' % data

    def __len__(self):
        self._fetch_all()
+5 −5
Original line number Diff line number Diff line
@@ -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'
+8 −9
Original line number Diff line number Diff line
@@ -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')
+1 −1
Original line number Diff line number Diff line
@@ -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
------------------
+3 −3
Original line number Diff line number Diff line
@@ -382,7 +382,7 @@ be used to retrieve their associated ``TaggedItems``::
    >>> t2 = TaggedItem(content_object=b, tag='python')
    >>> t2.save()
    >>> b.tags.all()
    [<TaggedItem: django>, <TaggedItem: python>]
    <QuerySet [<TaggedItem: django>, <TaggedItem: python>]>

Defining :class:`~django.contrib.contenttypes.fields.GenericRelation` with
``related_query_name`` set allows querying from the related object::
@@ -394,7 +394,7 @@ from ``TaggedItem``::

    >>> # Get all tags belonging to books containing `django` in the url
    >>> TaggedItem.objects.filter(bookmarks__url__contains='django')
    [<TaggedItem: django>, <TaggedItem: python>]
    <QuerySet [<TaggedItem: django>, <TaggedItem: python>]>

Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey`
accepts the names of the content-type and object-ID fields as
@@ -418,7 +418,7 @@ same types of lookups manually::
    >>> bookmark_type = ContentType.objects.get_for_model(b)
    >>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id,
    ...                           object_id=b.id)
    [<TaggedItem: django>, <TaggedItem: python>]
    <QuerySet [<TaggedItem: django>, <TaggedItem: python>]>

Note that if the model in a
:class:`~django.contrib.contenttypes.fields.GenericRelation` uses a
Loading