mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Thanks Mariusz Felisiak and Simon Charette for reviews.
This commit is contained in:
parent
e46ca51c24
commit
8b040e3cbb
14 changed files with 354 additions and 82 deletions
|
@ -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
|
||||
--------------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue