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

@ -79,7 +79,7 @@ In a hurry? Here's how to do common aggregate queries, assuming the models above
>>> from django.db.models import Count
>>> pubs = Publisher.objects.annotate(num_books=Count('book'))
>>> pubs
[<Publisher BaloneyPress>, <Publisher SalamiPress>, ...]
<QuerySet [<Publisher: BaloneyPress>, <Publisher: SalamiPress>, ...]>
>>> pubs[0].num_books
73

View file

@ -93,36 +93,36 @@ Create and add a ``Publication`` to an ``Article`` in one step using
``Article`` objects have access to their related ``Publication`` objects::
>>> a1.publications.all()
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> a2.publications.all()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]>
``Publication`` objects have access to their related ``Article`` objects::
>>> p2.article_set.all()
[<Article: NASA uses Python>]
<QuerySet [<Article: NASA uses Python>]>
>>> p1.article_set.all()
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Publication.objects.get(id=4).article_set.all()
[<Article: NASA uses Python>]
<QuerySet [<Article: NASA uses Python>]>
Many-to-many relationships can be queried using :ref:`lookups across
relationships <lookups-that-span-relationships>`::
>>> Article.objects.filter(publications__id=1)
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications__pk=1)
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications=1)
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications=p1)
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications__title__startswith="Science")
[<Article: NASA uses Python>, <Article: NASA uses Python>]
<QuerySet [<Article: NASA uses Python>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications__title__startswith="Science").distinct()
[<Article: NASA uses Python>]
<QuerySet [<Article: NASA uses Python>]>
The :meth:`~django.db.models.query.QuerySet.count` function respects
:meth:`~django.db.models.query.QuerySet.distinct` as well::
@ -134,57 +134,57 @@ The :meth:`~django.db.models.query.QuerySet.count` function respects
1
>>> Article.objects.filter(publications__in=[1,2]).distinct()
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
>>> Article.objects.filter(publications__in=[p1,p2]).distinct()
[<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA uses Python>]>
Reverse m2m queries are supported (i.e., starting at the table that doesn't have
a :class:`~django.db.models.ManyToManyField`)::
>>> Publication.objects.filter(id=1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(pk=1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(article__headline__startswith="NASA")
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]>
>>> Publication.objects.filter(article__id=1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(article__pk=1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(article=1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(article=a1)
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
>>> Publication.objects.filter(article__in=[1,2]).distinct()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]>
>>> Publication.objects.filter(article__in=[a1,a2]).distinct()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>, <Publication: The Python Journal>]>
Excluding a related item works as you would expect, too (although the SQL
involved is a little complex)::
>>> Article.objects.exclude(publications=p2)
[<Article: Django lets you build Web apps easily>]
<QuerySet [<Article: Django lets you build Web apps easily>]>
If we delete a ``Publication``, its ``Articles`` won't be able to access it::
>>> p1.delete()
>>> Publication.objects.all()
[<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>]
<QuerySet [<Publication: Highlights for Children>, <Publication: Science News>, <Publication: Science Weekly>]>
>>> a1 = Article.objects.get(pk=1)
>>> a1.publications.all()
[]
<QuerySet []>
If we delete an ``Article``, its ``Publications`` won't be able to access it::
>>> a2.delete()
>>> Article.objects.all()
[<Article: Django lets you build Web apps easily>]
<QuerySet [<Article: Django lets you build Web apps easily>]>
>>> p2.article_set.all()
[]
<QuerySet []>
Adding via the 'other' end of an m2m::
@ -192,61 +192,61 @@ Adding via the 'other' end of an m2m::
>>> a4.save()
>>> p2.article_set.add(a4)
>>> p2.article_set.all()
[<Article: NASA finds intelligent life on Earth>]
<QuerySet [<Article: NASA finds intelligent life on Earth>]>
>>> a4.publications.all()
[<Publication: Science News>]
<QuerySet [<Publication: Science News>]>
Adding via the other end using keywords::
>>> new_article = p2.article_set.create(headline='Oxygen-free diet works wonders')
>>> p2.article_set.all()
[<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>]
<QuerySet [<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>]>
>>> a5 = p2.article_set.all()[1]
>>> a5.publications.all()
[<Publication: Science News>]
<QuerySet [<Publication: Science News>]>
Removing ``Publication`` from an ``Article``::
>>> a4.publications.remove(p2)
>>> p2.article_set.all()
[<Article: Oxygen-free diet works wonders>]
<QuerySet [<Article: Oxygen-free diet works wonders>]>
>>> a4.publications.all()
[]
<QuerySet []>
And from the other end::
>>> p2.article_set.remove(a5)
>>> p2.article_set.all()
[]
<QuerySet []>
>>> a5.publications.all()
[]
<QuerySet []>
Relation sets can be assigned. Assignment clears any existing set members::
>>> a4.publications.all()
[<Publication: Science News>]
<QuerySet [<Publication: Science News>]>
>>> a4.publications = [p3]
>>> a4.publications.all()
[<Publication: Science Weekly>]
<QuerySet [<Publication: Science Weekly>]>
Relation sets can be cleared::
>>> p2.article_set.clear()
>>> p2.article_set.all()
[]
<QuerySet []>
And you can clear from the other end::
>>> p2.article_set.add(a4, a5)
>>> p2.article_set.all()
[<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>]
<QuerySet [<Article: NASA finds intelligent life on Earth>, <Article: Oxygen-free diet works wonders>]>
>>> a4.publications.all()
[<Publication: Science News>, <Publication: Science Weekly>]
<QuerySet [<Publication: Science News>, <Publication: Science Weekly>]>
>>> a4.publications.clear()
>>> a4.publications.all()
[]
<QuerySet []>
>>> p2.article_set.all()
[<Article: Oxygen-free diet works wonders>]
<QuerySet [<Article: Oxygen-free diet works wonders>]>
Recreate the ``Article`` and ``Publication`` we have deleted::
@ -261,17 +261,17 @@ go::
>>> Publication.objects.filter(title__startswith='Science').delete()
>>> Publication.objects.all()
[<Publication: Highlights for Children>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: The Python Journal>]>
>>> Article.objects.all()
[<Article: Django lets you build Web apps easily>, <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, <Article: Oxygen-free diet works wonders>]
<QuerySet [<Article: Django lets you build Web apps easily>, <Article: NASA finds intelligent life on Earth>, <Article: NASA uses Python>, <Article: Oxygen-free diet works wonders>]>
>>> a2.publications.all()
[<Publication: The Python Journal>]
<QuerySet [<Publication: The Python Journal>]>
Bulk delete some articles - references to deleted objects should go::
>>> q = Article.objects.filter(headline__startswith='Django')
>>> print(q)
[<Article: Django lets you build Web apps easily>]
<QuerySet [<Article: Django lets you build Web apps easily>]>
>>> q.delete()
After the :meth:`~django.db.models.query.QuerySet.delete`, the
@ -279,9 +279,9 @@ After the :meth:`~django.db.models.query.QuerySet.delete`, the
referenced objects should be gone::
>>> print(q)
[]
<QuerySet []>
>>> p1.article_set.all()
[<Article: NASA uses Python>]
<QuerySet [<Article: NASA uses Python>]>
An alternate to calling
:meth:`~django.db.models.fields.related.RelatedManager.clear` is to assign the
@ -289,11 +289,11 @@ empty set::
>>> p1.article_set = []
>>> p1.article_set.all()
[]
<QuerySet []>
>>> a2.publications = [p1, new_publication]
>>> a2.publications.all()
[<Publication: Highlights for Children>, <Publication: The Python Journal>]
<QuerySet [<Publication: Highlights for Children>, <Publication: The Python Journal>]>
>>> a2.publications = []
>>> a2.publications.all()
[]
<QuerySet []>

View file

@ -90,7 +90,7 @@ Create a new article, and add it to the article set::
>>> new_article2.reporter.id
1
>>> r.article_set.all()
[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
Add the same article to a different article set - check that it moves::
@ -108,9 +108,9 @@ Adding an object of the wrong type raises TypeError::
TypeError: 'Article' instance expected
>>> r.article_set.all()
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
>>> r2.article_set.all()
[<Article: Paul's story>]
<QuerySet [<Article: Paul's story>]>
>>> r.article_set.count()
2
@ -126,56 +126,56 @@ Use double underscores to separate relationships.
This works as many levels deep as you want. There's no limit. For example::
>>> r.article_set.filter(headline__startswith='This')
[<Article: This is a test>]
<QuerySet [<Article: This is a test>]>
# Find all Articles for any Reporter whose first name is "John".
>>> Article.objects.filter(reporter__first_name='John')
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
Exact match is implied here::
>>> Article.objects.filter(reporter__first_name='John')
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
Query twice over the related field. This translates to an AND condition in the
WHERE clause::
>>> Article.objects.filter(reporter__first_name='John', reporter__last_name='Smith')
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
For the related lookup you can supply a primary key value or pass the related
object explicitly::
>>> Article.objects.filter(reporter__pk=1)
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
>>> Article.objects.filter(reporter=1)
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
>>> Article.objects.filter(reporter=r)
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
>>> Article.objects.filter(reporter__in=[1,2]).distinct()
[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
>>> Article.objects.filter(reporter__in=[r,r2]).distinct()
[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
You can also use a queryset instead of a literal list of instances::
>>> Article.objects.filter(reporter__in=Reporter.objects.filter(first_name='John')).distinct()
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
Querying in the opposite direction::
>>> Reporter.objects.filter(article__pk=1)
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
>>> Reporter.objects.filter(article=1)
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
>>> Reporter.objects.filter(article=a)
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
>>> Reporter.objects.filter(article__headline__startswith='This')
[<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]>
>>> Reporter.objects.filter(article__headline__startswith='This').distinct()
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
Counting in the opposite direction works in conjunction with distinct()::
@ -187,30 +187,30 @@ Counting in the opposite direction works in conjunction with distinct()::
Queries can go round in circles::
>>> Reporter.objects.filter(article__reporter__first_name__startswith='John')
[<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>, <Reporter: John Smith>]>
>>> Reporter.objects.filter(article__reporter__first_name__startswith='John').distinct()
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
>>> Reporter.objects.filter(article__reporter=r).distinct()
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
If you delete a reporter, his articles will be deleted (assuming that the
ForeignKey was defined with :attr:`django.db.models.ForeignKey.on_delete` set to
``CASCADE``, which is the default)::
>>> Article.objects.all()
[<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: Paul's story>, <Article: This is a test>]>
>>> Reporter.objects.order_by('first_name')
[<Reporter: John Smith>, <Reporter: Paul Jones>]
<QuerySet [<Reporter: John Smith>, <Reporter: Paul Jones>]>
>>> r2.delete()
>>> Article.objects.all()
[<Article: John's second story>, <Article: This is a test>]
<QuerySet [<Article: John's second story>, <Article: This is a test>]>
>>> Reporter.objects.order_by('first_name')
[<Reporter: John Smith>]
<QuerySet [<Reporter: John Smith>]>
You can delete using a JOIN in the query::
>>> Reporter.objects.filter(article__headline__startswith='This').delete()
>>> Reporter.objects.all()
[]
<QuerySet []>
>>> Article.objects.all()
[]
<QuerySet []>

View file

@ -106,13 +106,13 @@ that there are two restaurants - Ace Hardware the Restaurant was created in the
call to r.place = p2::
>>> Restaurant.objects.all()
[<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]
<QuerySet [<Restaurant: Demon Dogs the restaurant>, <Restaurant: Ace Hardware the restaurant>]>
Place.objects.all() returns all Places, regardless of whether they have
Restaurants::
>>> Place.objects.order_by('name')
[<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]
<QuerySet [<Place: Ace Hardware the place>, <Place: Demon Dogs the place>]>
You can query the models using :ref:`lookups across relationships <lookups-that-span-relationships>`::
@ -121,9 +121,9 @@ You can query the models using :ref:`lookups across relationships <lookups-that-
>>> Restaurant.objects.get(place__pk=1)
<Restaurant: Demon Dogs the restaurant>
>>> Restaurant.objects.filter(place__name__startswith="Demon")
[<Restaurant: Demon Dogs the restaurant>]
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
>>> Restaurant.objects.exclude(place__address__contains="Ashland")
[<Restaurant: Demon Dogs the restaurant>]
<QuerySet [<Restaurant: Demon Dogs the restaurant>]>
This of course works in reverse::
@ -146,6 +146,6 @@ Add a Waiter to the Restaurant::
Query the waiters::
>>> Waiter.objects.filter(restaurant__place=p1)
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>
>>> Waiter.objects.filter(restaurant__place__name__startswith="Demon")
[<Waiter: Joe the waiter at Demon Dogs the restaurant>]
<QuerySet [<Waiter: Joe the waiter at Demon Dogs the restaurant>]>

View file

@ -501,14 +501,14 @@ the intermediate model::
... invite_reason="Needed a new drummer.")
>>> m1.save()
>>> beatles.members.all()
[<Person: Ringo Starr>]
<QuerySet [<Person: Ringo Starr>]>
>>> ringo.group_set.all()
[<Group: The Beatles>]
<QuerySet [<Group: The Beatles>]>
>>> m2 = Membership.objects.create(person=paul, group=beatles,
... date_joined=date(1960, 8, 1),
... invite_reason="Wanted to form a band.")
>>> beatles.members.all()
[<Person: Ringo Starr>, <Person: Paul McCartney>]
<QuerySet [<Person: Ringo Starr>, <Person: Paul McCartney>]>
Unlike normal many-to-many fields, you *can't* use ``add``, ``create``,
or assignment (i.e., ``beatles.members = [...]``) to create relationships::
@ -537,7 +537,7 @@ used to remove all many-to-many relationships for an instance::
>>> beatles.members.clear()
>>> # Note that this deletes the intermediate model instances
>>> Membership.objects.all()
[]
<QuerySet []>
Once you have established the many-to-many relationships by creating instances
of your intermediate model, you can issue queries. Just as with normal
@ -546,7 +546,7 @@ many-to-many-related model::
# Find all the groups with a member whose name starts with 'Paul'
>>> Group.objects.filter(members__name__startswith='Paul')
[<Group: The Beatles>]
<QuerySet [<Group: The Beatles>]>
As you are using an intermediate model, you can also query on its attributes::
@ -554,7 +554,7 @@ As you are using an intermediate model, you can also query on its attributes::
>>> Person.objects.filter(
... group__name='The Beatles',
... membership__date_joined__gt=date(1961,1,1))
[<Person: Ringo Starr]
<QuerySet [<Person: Ringo Starr]>
If you need to access a membership's information you may do so by directly
querying the ``Membership`` model::