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
155b31d4
Kaydet (Commit)
155b31d4
authored
Agu 07, 2018
tarafından
Raphael Michel
Kaydeden (comit)
Tim Graham
Agu 07, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #29648 -- Fixed crash when using subqueries inside datetime truncation functions.
üst
53e85705
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
4 deletions
+35
-4
datetime.py
django/db/models/functions/datetime.py
+0
-2
models.py
tests/db_functions/models.py
+1
-0
test_datetime.py
tests/db_functions/test_datetime.py
+34
-2
No files found.
django/db/models/functions/datetime.py
Dosyayı görüntüle @
155b31d4
...
...
@@ -164,8 +164,6 @@ class TruncBase(TimezoneMixin, Transform):
def
as_sql
(
self
,
compiler
,
connection
):
inner_sql
,
inner_params
=
compiler
.
compile
(
self
.
lhs
)
# Escape any params because trunc_sql will format the string.
inner_sql
=
inner_sql
.
replace
(
'
%
s'
,
'
%%
s'
)
if
isinstance
(
self
.
output_field
,
DateTimeField
):
tzname
=
self
.
get_tzname
()
sql
=
connection
.
ops
.
datetime_trunc_sql
(
self
.
kind
,
inner_sql
,
tzname
)
...
...
tests/db_functions/models.py
Dosyayı görüntüle @
155b31d4
...
...
@@ -32,6 +32,7 @@ class Fan(models.Model):
name
=
models
.
CharField
(
max_length
=
50
)
age
=
models
.
PositiveSmallIntegerField
(
default
=
30
)
author
=
models
.
ForeignKey
(
Author
,
models
.
CASCADE
,
related_name
=
'fans'
)
fan_since
=
models
.
DateTimeField
(
null
=
True
,
blank
=
True
)
def
__str__
(
self
):
return
self
.
name
...
...
tests/db_functions/test_datetime.py
Dosyayı görüntüle @
155b31d4
...
...
@@ -3,7 +3,9 @@ from datetime import datetime, timedelta
import
pytz
from
django.conf
import
settings
from
django.db.models
import
DateField
,
DateTimeField
,
IntegerField
,
TimeField
from
django.db.models
import
(
DateField
,
DateTimeField
,
IntegerField
,
Max
,
OuterRef
,
Subquery
,
TimeField
,
)
from
django.db.models.functions
import
(
Extract
,
ExtractDay
,
ExtractHour
,
ExtractMinute
,
ExtractMonth
,
ExtractQuarter
,
ExtractSecond
,
ExtractWeek
,
ExtractWeekDay
,
ExtractYear
,
...
...
@@ -15,7 +17,7 @@ from django.test import (
)
from
django.utils
import
timezone
from
.models
import
DTModel
from
.models
import
Author
,
DTModel
,
Fan
def
truncate_to
(
value
,
kind
,
tzinfo
=
None
):
...
...
@@ -854,6 +856,36 @@ class DateFunctionTests(TestCase):
with
self
.
assertRaisesMessage
(
ValueError
,
"Cannot truncate DateField 'start_date' to DateTimeField"
):
list
(
DTModel
.
objects
.
annotate
(
truncated
=
TruncSecond
(
'start_date'
,
output_field
=
DateField
())))
def
test_trunc_subquery_with_parameters
(
self
):
author_1
=
Author
.
objects
.
create
(
name
=
'J. R. R. Tolkien'
)
author_2
=
Author
.
objects
.
create
(
name
=
'G. R. R. Martin'
)
fan_since_1
=
datetime
(
2016
,
2
,
3
,
15
,
0
,
0
)
fan_since_2
=
datetime
(
2015
,
2
,
3
,
15
,
0
,
0
)
fan_since_3
=
datetime
(
2017
,
2
,
3
,
15
,
0
,
0
)
if
settings
.
USE_TZ
:
fan_since_1
=
timezone
.
make_aware
(
fan_since_1
,
is_dst
=
False
)
fan_since_2
=
timezone
.
make_aware
(
fan_since_2
,
is_dst
=
False
)
fan_since_3
=
timezone
.
make_aware
(
fan_since_3
,
is_dst
=
False
)
Fan
.
objects
.
create
(
author
=
author_1
,
name
=
'Tom'
,
fan_since
=
fan_since_1
)
Fan
.
objects
.
create
(
author
=
author_1
,
name
=
'Emma'
,
fan_since
=
fan_since_2
)
Fan
.
objects
.
create
(
author
=
author_2
,
name
=
'Isabella'
,
fan_since
=
fan_since_3
)
inner
=
Fan
.
objects
.
filter
(
author
=
OuterRef
(
'pk'
),
name__in
=
(
'Emma'
,
'Isabella'
,
'Tom'
)
)
.
values
(
'author'
)
.
annotate
(
newest_fan
=
Max
(
'fan_since'
))
.
values
(
'newest_fan'
)
outer
=
Author
.
objects
.
annotate
(
newest_fan_year
=
TruncYear
(
Subquery
(
inner
,
output_field
=
DateTimeField
()))
)
tz
=
pytz
.
UTC
if
settings
.
USE_TZ
else
None
self
.
assertSequenceEqual
(
outer
.
order_by
(
'name'
)
.
values
(
'name'
,
'newest_fan_year'
),
[
{
'name'
:
'G. R. R. Martin'
,
'newest_fan_year'
:
datetime
(
2017
,
1
,
1
,
0
,
0
,
tzinfo
=
tz
)},
{
'name'
:
'J. R. R. Tolkien'
,
'newest_fan_year'
:
datetime
(
2016
,
1
,
1
,
0
,
0
,
tzinfo
=
tz
)},
]
)
@override_settings
(
USE_TZ
=
True
,
TIME_ZONE
=
'UTC'
)
class
DateFunctionWithTimeZoneTests
(
DateFunctionTests
):
...
...
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