diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-10-25 12:27:27 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-10-25 12:27:56 +0200 |
| commit | 415ef34c4c2d4e9416ecf04ddf8cfb33585f1934 (patch) | |
| tree | 12194fbcd557df1cd3ea5099307da032ad360812 /docs/topics | |
| parent | 8b18e0bb3ba8bb1f51e15487a6d5402853e637ae (diff) | |
[5.0.x] Added missing pycon directives in various docs.
Backport of 718b32c6918037cfc746d7867333d79a3c887a8c from main
Diffstat (limited to 'docs/topics')
| -rw-r--r-- | docs/topics/cache.txt | 16 | ||||
| -rw-r--r-- | docs/topics/db/aggregation.txt | 14 | ||||
| -rw-r--r-- | docs/topics/db/queries.txt | 20 |
3 files changed, 35 insertions, 15 deletions
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: 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, ... ) |
