Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
62917cee
Kaydet (Commit)
62917cee
authored
Haz 20, 2017
tarafından
Tomer Chachamu
Kaydeden (comit)
Tim Graham
Tem 18, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Doc'd the need to remove default ordering on Subquery aggregates.
üst
12812f6b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
5 deletions
+6
-5
expressions.txt
docs/ref/models/expressions.txt
+6
-5
No files found.
docs/ref/models/expressions.txt
Dosyayı görüntüle @
62917cee
...
@@ -601,15 +601,16 @@ Assuming both models have a ``length`` field, to find posts where the post
...
@@ -601,15 +601,16 @@ Assuming both models have a ``length`` field, to find posts where the post
length is greater than the total length of all combined comments::
length is greater than the total length of all combined comments::
>>> from django.db.models import OuterRef, Subquery, Sum
>>> from django.db.models import OuterRef, Subquery, Sum
>>> comments = Comment.objects.filter(post=OuterRef('pk')).values('post')
>>> comments = Comment.objects.filter(post=OuterRef('pk')).
order_by().
values('post')
>>> total_comments = comments.annotate(total=Sum('length')).values('total')
>>> total_comments = comments.annotate(total=Sum('length')).values('total')
>>> Post.objects.filter(length__gt=Subquery(total_comments))
>>> Post.objects.filter(length__gt=Subquery(total_comments))
The initial ``filter(...)`` limits the subquery to the relevant parameters.
The initial ``filter(...)`` limits the subquery to the relevant parameters.
``values('post')`` aggregates comments by ``Post``. Finally, ``annotate(...)``
``order_by()`` removes the default :attr:`~django.db.models.Options.ordering`
performs the aggregation. The order in which these queryset methods are applied
(if any) on the ``Comment`` model. ``values('post')`` aggregates comments by
is important. In this case, since the subquery must be limited to a single
``Post``. Finally, ``annotate(...)`` performs the aggregation. The order in
column, ``values('total')`` is required.
which these queryset methods are applied is important. In this case, since the
subquery must be limited to a single column, ``values('total')`` is required.
This is the only way to perform an aggregation within a ``Subquery``, as
This is the only way to perform an aggregation within a ``Subquery``, as
using :meth:`~.QuerySet.aggregate` attempts to evaluate the queryset (and if
using :meth:`~.QuerySet.aggregate` attempts to evaluate the queryset (and if
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment