Skip to content

Commit a0be764

Browse files
committed
add TruncDate support
1 parent 1ac53e3 commit a0be764

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

django_mongodb/features.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,7 @@ def django_test_expected_failures(self):
498498
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
499499
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
500500
},
501-
"TruncDate database function not supported.": {
502-
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_date_from_database",
503-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_func",
504-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_none",
505-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_lookup_name_sql_injection",
506-
"expressions.tests.FieldTransformTests.test_multiple_transforms_in_values",
501+
"TruncDate database function with timezone not supported.": {
507502
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
508503
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
509504
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",

django_mongodb/functions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ExtractYear,
1616
Now,
1717
TruncBase,
18+
TruncDate,
1819
)
1920
from django.db.models.functions.math import Ceil, Cot, Degrees, Log, Power, Radians, Random, Round
2021
from django.db.models.functions.text import (
@@ -191,6 +192,13 @@ def trunc(self, compiler, connection):
191192
return {"$dateTrunc": lhs_mql}
192193

193194

195+
def trunc_date(self, compiler, connection):
196+
# Cast to date rather than truncate to date.
197+
lhs_mql = process_lhs(self, compiler, connection)
198+
# tzname = self.get_tzname()
199+
return {"$toDate": lhs_mql}
200+
201+
194202
def register_functions():
195203
Cast.as_mql = cast
196204
Concat.as_mql = concat
@@ -212,4 +220,5 @@ def register_functions():
212220
Substr.as_mql = substr
213221
Trim.as_mql = trim("trim")
214222
TruncBase.as_mql = trunc
223+
TruncDate.as_mql = trunc_date
215224
Upper.as_mql = preserve_null("toUpper")

0 commit comments

Comments
 (0)