1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Added missing pycon directives in various docs.

This commit is contained in:
Mariusz Felisiak
2023-10-25 12:27:27 +02:00
committed by GitHub
parent ee104251c4
commit 718b32c691
11 changed files with 295 additions and 130 deletions

View File

@@ -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::

View File

@@ -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.

View File

@@ -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,
... )