diff options
| author | Tim Graham <timograham@gmail.com> | 2015-10-05 19:07:34 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-10-06 12:38:34 -0400 |
| commit | e0837f2cb12de5e95e621d19b186b0da43bcdee2 (patch) | |
| tree | ea6ae0b150304ed18d93fb8c3ee3b7defbda0e26 /docs/topics/db/examples | |
| parent | 3543fec3b739864c52de0a116dde3b0e5e885799 (diff) | |
Fixed #25508 -- Modified QuerySet.__repr__() to disambiguate it from a list.
Diffstat (limited to 'docs/topics/db/examples')
| -rw-r--r-- | docs/topics/db/examples/many_to_many.txt | 102 | ||||
| -rw-r--r-- | docs/topics/db/examples/many_to_one.txt | 54 | ||||
| -rw-r--r-- | docs/topics/db/examples/one_to_one.txt | 12 |
3 files changed, 84 insertions, 84 deletions
diff --git a/docs/topics/db/examples/many_to_many.txt b/docs/topics/db/examples/many_to_many.txt index 79605ac158..cbf8da99e2 100644 --- a/docs/topics/db/examples/many_to_many.txt +++ b/docs/topics/db/examples/many_to_many.txt @@ -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 []> diff --git a/docs/topics/db/examples/many_to_one.txt b/docs/topics/db/examples/many_to_one.txt index 93cc3f093f..64f6ab1f1b 100644 --- a/docs/topics/db/examples/many_to_one.txt +++ b/docs/topics/db/examples/many_to_one.txt @@ -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 []> diff --git a/docs/topics/db/examples/one_to_one.txt b/docs/topics/db/examples/one_to_one.txt index dbf46fc079..7e373a98e1 100644 --- a/docs/topics/db/examples/one_to_one.txt +++ b/docs/topics/db/examples/one_to_one.txt @@ -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>]> |
