summaryrefslogtreecommitdiff
path: root/docs/topics/db
diff options
context:
space:
mode:
Diffstat (limited to 'docs/topics/db')
-rw-r--r--docs/topics/db/aggregation.txt14
-rw-r--r--docs/topics/db/queries.txt20
2 files changed, 23 insertions, 11 deletions
diff --git a/docs/topics/db/aggregation.txt b/docs/topics/db/aggregation.txt
index 9f98504c0c..cd53f73c74 100644
--- a/docs/topics/db/aggregation.txt
+++ b/docs/topics/db/aggregation.txt
@@ -222,12 +222,14 @@ Combining multiple aggregations
Combining multiple aggregations with ``annotate()`` will :ticket:`yield the
wrong results <10060>` because joins are used instead of subqueries:
+.. code-block:: pycon
+
>>> book = Book.objects.first()
>>> book.authors.count()
2
>>> book.store_set.count()
3
- >>> q = Book.objects.annotate(Count('authors'), Count('store'))
+ >>> q = Book.objects.annotate(Count("authors"), Count("store"))
>>> q[0].authors__count
6
>>> q[0].store__count
@@ -237,7 +239,11 @@ For most aggregates, there is no way to avoid this problem, however, the
:class:`~django.db.models.Count` aggregate has a ``distinct`` parameter that
may help:
- >>> q = Book.objects.annotate(Count('authors', distinct=True), Count('store', distinct=True))
+.. code-block:: pycon
+
+ >>> q = Book.objects.annotate(
+ ... Count("authors", distinct=True), Count("store", distinct=True)
+ ... )
>>> q[0].authors__count
2
>>> q[0].store__count
@@ -514,7 +520,9 @@ the annotation is computed over all members of the group.
For example, consider an author query that attempts to find out the average
rating of books written by each author:
- >>> Author.objects.annotate(average_rating=Avg('book__rating'))
+.. code-block:: pycon
+
+ >>> Author.objects.annotate(average_rating=Avg("book__rating"))
This will return one result for each author in the database, annotated with
their average book rating.
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
index 6dfe97f317..65e84ecdc4 100644
--- a/docs/topics/db/queries.txt
+++ b/docs/topics/db/queries.txt
@@ -448,6 +448,8 @@ can specify the field name suffixed with ``_id``. In this case, the value
parameter is expected to contain the raw value of the foreign model's primary
key. For example:
+.. code-block:: pycon
+
>>> Entry.objects.filter(blog_id=4)
If you pass an invalid keyword argument, a lookup function will raise
@@ -610,40 +612,42 @@ contained in a single :meth:`~django.db.models.query.QuerySet.filter` call.
As the second (more permissive) query chains multiple filters, it performs
multiple joins to the primary model, potentially yielding duplicates.
+ .. code-block:: pycon
+
>>> from datetime import date
- >>> beatles = Blog.objects.create(name='Beatles Blog')
- >>> pop = Blog.objects.create(name='Pop Music Blog')
+ >>> beatles = Blog.objects.create(name="Beatles Blog")
+ >>> pop = Blog.objects.create(name="Pop Music Blog")
>>> Entry.objects.create(
... blog=beatles,
- ... headline='New Lennon Biography',
+ ... headline="New Lennon Biography",
... pub_date=date(2008, 6, 1),
... )
<Entry: New Lennon Biography>
>>> Entry.objects.create(
... blog=beatles,
- ... headline='New Lennon Biography in Paperback',
+ ... headline="New Lennon Biography in Paperback",
... pub_date=date(2009, 6, 1),
... )
<Entry: New Lennon Biography in Paperback>
>>> Entry.objects.create(
... blog=pop,
- ... headline='Best Albums of 2008',
+ ... headline="Best Albums of 2008",
... pub_date=date(2008, 12, 15),
... )
<Entry: Best Albums of 2008>
>>> Entry.objects.create(
... blog=pop,
- ... headline='Lennon Would Have Loved Hip Hop',
+ ... headline="Lennon Would Have Loved Hip Hop",
... pub_date=date(2020, 4, 1),
... )
<Entry: Lennon Would Have Loved Hip Hop>
>>> Blog.objects.filter(
- ... entry__headline__contains='Lennon',
+ ... entry__headline__contains="Lennon",
... entry__pub_date__year=2008,
... )
<QuerySet [<Blog: Beatles Blog>]>
>>> Blog.objects.filter(
- ... entry__headline__contains='Lennon',
+ ... entry__headline__contains="Lennon",
... ).filter(
... entry__pub_date__year=2008,
... )