From 415ef34c4c2d4e9416ecf04ddf8cfb33585f1934 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 25 Oct 2023 12:27:27 +0200 Subject: [5.0.x] Added missing pycon directives in various docs. Backport of 718b32c6918037cfc746d7867333d79a3c887a8c from main --- docs/topics/cache.txt | 16 ++++++++++++---- docs/topics/db/aggregation.txt | 14 +++++++++++--- docs/topics/db/queries.txt | 20 ++++++++++++-------- 3 files changed, 35 insertions(+), 15 deletions(-) (limited to 'docs/topics') diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index abdbea0e0a..5935c3e27f 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -872,9 +872,11 @@ Accessing the cache requests for the same alias in the same thread will return the same object. + .. code-block:: pycon + >>> from django.core.cache import caches - >>> cache1 = caches['myalias'] - >>> cache2 = caches['myalias'] + >>> cache1 = caches["myalias"] + >>> cache2 = caches["myalias"] >>> cache1 is cache2 True @@ -906,11 +908,15 @@ The basic interface is: .. method:: cache.set(key, value, timeout=DEFAULT_TIMEOUT, version=None) - >>> cache.set('my_key', 'hello, world!', 30) +.. code-block:: pycon + + >>> cache.set("my_key", "hello, world!", 30) .. method:: cache.get(key, default=None, version=None) - >>> cache.get('my_key') +.. code-block:: pycon + + >>> cache.get("my_key") 'hello, world!' ``key`` should be a ``str``, and ``value`` can be any picklable Python object. @@ -1100,6 +1106,8 @@ nonexistent cache key: You can close the connection to your cache with ``close()`` if implemented by the cache backend. +.. code-block:: pycon + >>> cache.close() .. note:: 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.objects.create( ... blog=beatles, - ... headline='New Lennon Biography in Paperback', + ... headline="New Lennon Biography in Paperback", ... pub_date=date(2009, 6, 1), ... ) >>> Entry.objects.create( ... blog=pop, - ... headline='Best Albums of 2008', + ... headline="Best Albums of 2008", ... pub_date=date(2008, 12, 15), ... ) >>> 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), ... ) >>> Blog.objects.filter( - ... entry__headline__contains='Lennon', + ... entry__headline__contains="Lennon", ... entry__pub_date__year=2008, ... ) ]> >>> Blog.objects.filter( - ... entry__headline__contains='Lennon', + ... entry__headline__contains="Lennon", ... ).filter( ... entry__pub_date__year=2008, ... ) -- cgit v1.3