Fixed #33958 -- Added imports to examples in "Expressions can reference transforms" section.

This commit is contained in:
Iván Camilo Triviño López 2022-08-29 23:03:53 -05:00 committed by GitHub
parent 0010721e89
commit 411a6ec93a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 0 deletions

View File

@ -701,15 +701,18 @@ Django supports using transforms in expressions.
For example, to find all ``Entry`` objects published in the same year as they For example, to find all ``Entry`` objects published in the same year as they
were last modified:: were last modified::
>>> from django.db.models import F
>>> Entry.objects.filter(pub_date__year=F('mod_date__year')) >>> Entry.objects.filter(pub_date__year=F('mod_date__year'))
To find the earliest year an entry was published, we can issue the query:: To find the earliest year an entry was published, we can issue the query::
>>> from django.db.models import Min
>>> Entry.objects.aggregate(first_published_year=Min('pub_date__year')) >>> Entry.objects.aggregate(first_published_year=Min('pub_date__year'))
This example finds the value of the highest rated entry and the total number This example finds the value of the highest rated entry and the total number
of comments on all entries for each year:: of comments on all entries for each year::
>>> from django.db.models import OuterRef, Subquery, Sum
>>> Entry.objects.values('pub_date__year').annotate( >>> Entry.objects.values('pub_date__year').annotate(
... top_rating=Subquery( ... top_rating=Subquery(
... Entry.objects.filter( ... Entry.objects.filter(