summaryrefslogtreecommitdiff
path: root/docs/topics
diff options
context:
space:
mode:
authorIan Foote <python@ian.feete.org>2020-11-15 22:43:47 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-11-27 20:42:04 +0100
commit8b040e3cbbb2e81420e777afc3ca48a1c8f4dd5a (patch)
tree32ed8b5456c9ce569643a1ebb34491a1c9b6fa01 /docs/topics
parente46ca51c249677c52e04db28fc0c60ae1948b3b2 (diff)
Fixed #25534, Fixed #31639 -- Added support for transform references in expressions.
Thanks Mariusz Felisiak and Simon Charette for reviews.
Diffstat (limited to 'docs/topics')
-rw-r--r--docs/topics/db/queries.txt30
1 files changed, 30 insertions, 0 deletions
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
index d29882e342..c92b8b0944 100644
--- a/docs/topics/db/queries.txt
+++ b/docs/topics/db/queries.txt
@@ -669,6 +669,36 @@ The ``F()`` objects support bitwise operations by ``.bitand()``, ``.bitor()``,
Support for ``.bitxor()`` was added.
+.. _using-transforms-in-expressions:
+
+Expressions can reference transforms
+------------------------------------
+
+.. versionadded: 3.2
+
+Django supports using transforms in expressions.
+
+For example, to find all ``Entry`` objects published in the same year as they
+were last modified::
+
+ >>> Entry.objects.filter(pub_date__year=F('mod_date__year'))
+
+To find the earliest year an entry was published, we can issue the query::
+
+ >>> Entry.objects.aggregate(first_published_year=Min('pub_date__year'))
+
+This example finds the value of the highest rated entry and the total number
+of comments on all entries for each year::
+
+ >>> Entry.objects.values('pub_date__year').annotate(
+ ... top_rating=Subquery(
+ ... Entry.objects.filter(
+ ... pub_date__year=OuterRef('pub_date__year'),
+ ... ).order_by('-rating').values('rating')[:1]
+ ... ),
+ ... total_comments=Sum('number_of_comments'),
+ ... )
+
The ``pk`` lookup shortcut
--------------------------