From e0837f2cb12de5e95e621d19b186b0da43bcdee2 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 5 Oct 2015 19:07:34 -0400 Subject: Fixed #25508 -- Modified QuerySet.__repr__() to disambiguate it from a list. --- docs/topics/db/examples/many_to_many.txt | 102 +++++++++++++++---------------- docs/topics/db/examples/many_to_one.txt | 54 ++++++++-------- docs/topics/db/examples/one_to_one.txt | 12 ++-- 3 files changed, 84 insertions(+), 84 deletions(-) (limited to 'docs/topics/db/examples') 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() - [] + ]> >>> a2.publications.all() - [, , , ] + , , , ]> ``Publication`` objects have access to their related ``Article`` objects:: >>> p2.article_set.all() - [] + ]> >>> p1.article_set.all() - [, ] + , ]> >>> Publication.objects.get(id=4).article_set.all() - [] + ]> Many-to-many relationships can be queried using :ref:`lookups across relationships `:: >>> Article.objects.filter(publications__id=1) - [, ] + , ]> >>> Article.objects.filter(publications__pk=1) - [, ] + , ]> >>> Article.objects.filter(publications=1) - [, ] + , ]> >>> Article.objects.filter(publications=p1) - [, ] + , ]> >>> Article.objects.filter(publications__title__startswith="Science") - [, ] + , ]> >>> Article.objects.filter(publications__title__startswith="Science").distinct() - [] + ]> 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.objects.filter(publications__in=[p1,p2]).distinct() - [, ] + , ]> 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.objects.filter(pk=1) - [] + ]> >>> Publication.objects.filter(article__headline__startswith="NASA") - [, , , ] + , , , ]> >>> Publication.objects.filter(article__id=1) - [] + ]> >>> Publication.objects.filter(article__pk=1) - [] + ]> >>> Publication.objects.filter(article=1) - [] + ]> >>> Publication.objects.filter(article=a1) - [] + ]> >>> Publication.objects.filter(article__in=[1,2]).distinct() - [, , , ] + , , , ]> >>> Publication.objects.filter(article__in=[a1,a2]).distinct() - [, , , ] + , , , ]> Excluding a related item works as you would expect, too (although the SQL involved is a little complex):: >>> Article.objects.exclude(publications=p2) - [] + ]> If we delete a ``Publication``, its ``Articles`` won't be able to access it:: >>> p1.delete() >>> Publication.objects.all() - [, , ] + , , ]> >>> a1 = Article.objects.get(pk=1) >>> a1.publications.all() - [] + If we delete an ``Article``, its ``Publications`` won't be able to access it:: >>> a2.delete() >>> Article.objects.all() - [] + ]> >>> p2.article_set.all() - [] + 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() - [] + ]> >>> a4.publications.all() - [] + ]> Adding via the other end using keywords:: >>> new_article = p2.article_set.create(headline='Oxygen-free diet works wonders') >>> p2.article_set.all() - [, ] + , ]> >>> a5 = p2.article_set.all()[1] >>> a5.publications.all() - [] + ]> Removing ``Publication`` from an ``Article``:: >>> a4.publications.remove(p2) >>> p2.article_set.all() - [] + ]> >>> a4.publications.all() - [] + And from the other end:: >>> p2.article_set.remove(a5) >>> p2.article_set.all() - [] + >>> a5.publications.all() - [] + Relation sets can be assigned. Assignment clears any existing set members:: >>> a4.publications.all() - [] + ]> >>> a4.publications = [p3] >>> a4.publications.all() - [] + ]> Relation sets can be cleared:: >>> p2.article_set.clear() >>> p2.article_set.all() - [] + And you can clear from the other end:: >>> p2.article_set.add(a4, a5) >>> p2.article_set.all() - [, ] + , ]> >>> a4.publications.all() - [, ] + , ]> >>> a4.publications.clear() >>> a4.publications.all() - [] + >>> p2.article_set.all() - [] + ]> Recreate the ``Article`` and ``Publication`` we have deleted:: @@ -261,17 +261,17 @@ go:: >>> Publication.objects.filter(title__startswith='Science').delete() >>> Publication.objects.all() - [, ] + , ]> >>> Article.objects.all() - [, , , ] + , , , ]> >>> a2.publications.all() - [] + ]> Bulk delete some articles - references to deleted objects should go:: >>> q = Article.objects.filter(headline__startswith='Django') >>> print(q) - [] + ]> >>> 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) - [] + >>> p1.article_set.all() - [] + ]> 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() - [] + >>> a2.publications = [p1, new_publication] >>> a2.publications.all() - [, ] + , ]> >>> a2.publications = [] >>> a2.publications.all() - [] + 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() - [, , ] + , , ]> 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() - [, ] + , ]> >>> r2.article_set.all() - [] + ]> >>> 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') - [] + ]> # Find all Articles for any Reporter whose first name is "John". >>> Article.objects.filter(reporter__first_name='John') - [, ] + , ]> Exact match is implied here:: >>> Article.objects.filter(reporter__first_name='John') - [, ] + , ]> 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') - [, ] + , ]> For the related lookup you can supply a primary key value or pass the related object explicitly:: >>> Article.objects.filter(reporter__pk=1) - [, ] + , ]> >>> Article.objects.filter(reporter=1) - [, ] + , ]> >>> Article.objects.filter(reporter=r) - [, ] + , ]> >>> Article.objects.filter(reporter__in=[1,2]).distinct() - [, , ] + , , ]> >>> Article.objects.filter(reporter__in=[r,r2]).distinct() - [, , ] + , , ]> 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() - [, ] + , ]> Querying in the opposite direction:: >>> Reporter.objects.filter(article__pk=1) - [] + ]> >>> Reporter.objects.filter(article=1) - [] + ]> >>> Reporter.objects.filter(article=a) - [] + ]> >>> Reporter.objects.filter(article__headline__startswith='This') - [, , ] + , , ]> >>> Reporter.objects.filter(article__headline__startswith='This').distinct() - [] + ]> 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.objects.filter(article__reporter__first_name__startswith='John').distinct() - [] + ]> >>> Reporter.objects.filter(article__reporter=r).distinct() - [] + ]> 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() - [, , ] + , , ]> >>> Reporter.objects.order_by('first_name') - [, ] + , ]> >>> r2.delete() >>> Article.objects.all() - [, ] + , ]> >>> Reporter.objects.order_by('first_name') - [] + ]> You can delete using a JOIN in the query:: >>> Reporter.objects.filter(article__headline__startswith='This').delete() >>> Reporter.objects.all() - [] + >>> Article.objects.all() - [] + 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() - [, ] + , ]> Place.objects.all() returns all Places, regardless of whether they have Restaurants:: >>> Place.objects.order_by('name') - [, ] + , ]> You can query the models using :ref:`lookups across relationships `:: @@ -121,9 +121,9 @@ You can query the models using :ref:`lookups across relationships >> Restaurant.objects.get(place__pk=1) >>> Restaurant.objects.filter(place__name__startswith="Demon") - [] + ]> >>> Restaurant.objects.exclude(place__address__contains="Ashland") - [] + ]> 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.objects.filter(restaurant__place__name__startswith="Demon") - [] + ]> -- cgit v1.3