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
b28c6ca7
Kaydet (Commit)
b28c6ca7
authored
Kas 15, 2016
tarafından
Jonatas CD
Kaydeden (comit)
Tim Graham
Kas 17, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27482 -- Doc'd an example of Case() in QuerySet.filter().
üst
721f0ca8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
0 deletions
+28
-0
conditional-expressions.txt
docs/ref/models/conditional-expressions.txt
+14
-0
tests.py
tests/expressions_case/tests.py
+14
-0
No files found.
docs/ref/models/conditional-expressions.txt
Dosyayı görüntüle @
b28c6ca7
...
...
@@ -141,6 +141,20 @@ the ``Client`` has been with us, we could do so using lookups::
both Jane Doe and Jack Black. This works just like an :keyword:`if` ...
:keyword:`elif` ... :keyword:`else` statement in ``Python``.
``Case()`` also works in a ``filter()`` clause. For example, to find gold
clients that registered more than a month ago and platinum clients that
registered more than a year ago::
>>> a_month_ago = date.today() - timedelta(days=30)
>>> a_year_ago = date.today() - timedelta(days=365)
>>> Client.objects.filter(
... registered_on__lte=Case(
... When(account_type=Client.GOLD, then=a_month_ago),
... When(account_type=Client.PLATINUM, then=a_year_ago),
... ),
... ).values_list('name', 'account_type')
[('Jack Black', 'P')]
Advanced queries
================
...
...
tests/expressions_case/tests.py
Dosyayı görüntüle @
b28c6ca7
...
...
@@ -1289,3 +1289,17 @@ class CaseDocumentationExamples(TestCase):
),
{
'regular'
:
2
,
'gold'
:
1
,
'platinum'
:
3
}
)
def
test_filter_example
(
self
):
a_month_ago
=
date
.
today
()
-
timedelta
(
days
=
30
)
a_year_ago
=
date
.
today
()
-
timedelta
(
days
=
365
)
self
.
assertQuerysetEqual
(
Client
.
objects
.
filter
(
registered_on__lte
=
Case
(
When
(
account_type
=
Client
.
GOLD
,
then
=
a_month_ago
),
When
(
account_type
=
Client
.
PLATINUM
,
then
=
a_year_ago
),
),
),
[(
'Jack Black'
,
'P'
)],
transform
=
attrgetter
(
'name'
,
'account_type'
)
)
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