1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

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

@@ -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 []>