summaryrefslogtreecommitdiff
path: root/docs/ref/models
diff options
context:
space:
mode:
authorJoseph Victor Zammit <jvzammit@gmail.com>2023-01-23 21:29:05 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-28 12:54:33 +0100
commit5bdd6223a24b2bcd0ee32251d6f3ce20e934a1dd (patch)
tree3c9b132c5e77bdcde426764aa9fddecaf90ae2af /docs/ref/models
parentfae76b81ce6d13245abc42752f0a82f16a9ba35f (diff)
[4.2.x] Refs #34140 -- Corrected rst code-block and various formatting issues in docs.
Backport of ba755ca13123d2691a0926ddb64e5d0a2906a880 from main
Diffstat (limited to 'docs/ref/models')
-rw-r--r--docs/ref/models/database-functions.txt4
-rw-r--r--docs/ref/models/expressions.txt96
-rw-r--r--docs/ref/models/relations.txt3
3 files changed, 53 insertions, 50 deletions
diff --git a/docs/ref/models/database-functions.txt b/docs/ref/models/database-functions.txt
index f6d597a266..765a988d08 100644
--- a/docs/ref/models/database-functions.txt
+++ b/docs/ref/models/database-functions.txt
@@ -1905,7 +1905,9 @@ more frequently.
.. class:: PercentRank(*expressions, **extra)
Computes the relative rank of the rows in the frame clause. This computation is
-equivalent to evaluating::
+equivalent to evaluating:
+
+.. code-block:: text
(rank - 1) / (total rows - 1)
diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt
index 6a59822e8e..d2f2d0e8b9 100644
--- a/docs/ref/models/expressions.txt
+++ b/docs/ref/models/expressions.txt
@@ -21,20 +21,20 @@ constants, variables, and even other expressions.
Some examples
=============
-.. code-block:: python
+.. code-block:: pycon
- from django.db.models import Count, F, Value
- from django.db.models.functions import Length, Upper
- from django.db.models.lookups import GreaterThan
+ >>> from django.db.models import Count, F, Value
+ >>> from django.db.models.functions import Length, Upper
+ >>> from django.db.models.lookups import GreaterThan
# Find companies that have more employees than chairs.
- Company.objects.filter(num_employees__gt=F('num_chairs'))
+ >>> Company.objects.filter(num_employees__gt=F('num_chairs'))
# Find companies that have at least twice as many employees
# as chairs. Both the querysets below are equivalent.
- Company.objects.filter(num_employees__gt=F('num_chairs') * 2)
- Company.objects.filter(
- num_employees__gt=F('num_chairs') + F('num_chairs'))
+ >>> Company.objects.filter(num_employees__gt=F('num_chairs') * 2)
+ >>> Company.objects.filter(
+ ... num_employees__gt=F('num_chairs') + F('num_chairs'))
# How many chairs are needed for each company to seat all employees?
>>> company = Company.objects.filter(
@@ -817,12 +817,12 @@ the same studio in the same genre and release year:
>>> from django.db.models import Avg, F, Window
>>> Movie.objects.annotate(
- >>> avg_rating=Window(
- >>> expression=Avg('rating'),
- >>> partition_by=[F('studio'), F('genre')],
- >>> order_by='released__year',
- >>> ),
- >>> )
+ ... avg_rating=Window(
+ ... expression=Avg('rating'),
+ ... partition_by=[F('studio'), F('genre')],
+ ... order_by='released__year',
+ ... ),
+ ... )
This allows you to check if a movie is rated better or worse than its peers.
@@ -837,20 +837,20 @@ to reduce repetition:
>>> from django.db.models import Avg, F, Max, Min, Window
>>> window = {
- >>> 'partition_by': [F('studio'), F('genre')],
- >>> 'order_by': 'released__year',
- >>> }
+ ... 'partition_by': [F('studio'), F('genre')],
+ ... 'order_by': 'released__year',
+ ... }
>>> Movie.objects.annotate(
- >>> avg_rating=Window(
- >>> expression=Avg('rating'), **window,
- >>> ),
- >>> best=Window(
- >>> expression=Max('rating'), **window,
- >>> ),
- >>> worst=Window(
- >>> expression=Min('rating'), **window,
- >>> ),
- >>> )
+ ... avg_rating=Window(
+ ... expression=Avg('rating'), **window,
+ ... ),
+ ... best=Window(
+ ... expression=Max('rating'), **window,
+ ... ),
+ ... worst=Window(
+ ... expression=Min('rating'), **window,
+ ... ),
+ ... )
Filtering against window functions is supported as long as lookups are not
disjunctive (not using ``OR`` or ``XOR`` as a connector) and against a queryset
@@ -864,13 +864,13 @@ from groups to be included:
.. code-block:: pycon
>>> qs = Movie.objects.annotate(
- >>> category_rank=Window(
- >>> Rank(), partition_by='category', order_by='-rating'
- >>> ),
- >>> scenes_count=Count('actors'),
- >>> ).filter(
- >>> Q(category_rank__lte=3) | Q(title__contains='Batman')
- >>> )
+ ... category_rank=Window(
+ ... Rank(), partition_by='category', order_by='-rating'
+ ... ),
+ ... scenes_count=Count('actors'),
+ ... ).filter(
+ ... Q(category_rank__lte=3) | Q(title__contains='Batman')
+ ... )
>>> list(qs)
NotImplementedError: Heterogeneous disjunctive predicates against window functions
are not implemented when performing conditional aggregation.
@@ -949,13 +949,13 @@ with the average rating of a movie's two prior and two following peers:
>>> from django.db.models import Avg, F, RowRange, Window
>>> Movie.objects.annotate(
- >>> avg_rating=Window(
- >>> expression=Avg('rating'),
- >>> partition_by=[F('studio'), F('genre')],
- >>> order_by='released__year',
- >>> frame=RowRange(start=-2, end=2),
- >>> ),
- >>> )
+ ... avg_rating=Window(
+ ... expression=Avg('rating'),
+ ... partition_by=[F('studio'), F('genre')],
+ ... order_by='released__year',
+ ... frame=RowRange(start=-2, end=2),
+ ... ),
+ ... )
If the database supports it, you can specify the start and end points based on
values of an expression in the partition. If the ``released`` field of the
@@ -967,13 +967,13 @@ released between twelve months before and twelve months after the each movie:
>>> from django.db.models import Avg, F, ValueRange, Window
>>> Movie.objects.annotate(
- >>> avg_rating=Window(
- >>> expression=Avg('rating'),
- >>> partition_by=[F('studio'), F('genre')],
- >>> order_by='released__year',
- >>> frame=ValueRange(start=-12, end=12),
- >>> ),
- >>> )
+ ... avg_rating=Window(
+ ... expression=Avg('rating'),
+ ... partition_by=[F('studio'), F('genre')],
+ ... order_by='released__year',
+ ... frame=ValueRange(start=-12, end=12),
+ ... ),
+ ... )
.. currentmodule:: django.db.models
diff --git a/docs/ref/models/relations.txt b/docs/ref/models/relations.txt
index d56b0fb33e..495c3164ee 100644
--- a/docs/ref/models/relations.txt
+++ b/docs/ref/models/relations.txt
@@ -24,7 +24,8 @@ Related objects reference
In the above example, the methods below will be available on
the manager ``blog.entry_set``.
- * Both sides of a :class:`~django.db.models.ManyToManyField` relation::
+ * Both sides of a :class:`~django.db.models.ManyToManyField` relation
+ ::
class Topping(models.Model):
# ...