Skip to content

Commit d9a981d

Browse files
committed
add TruncDate support
1 parent 500183c commit d9a981d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ Congratulations, your project is ready to go!
203203
- `Right`
204204
- `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512`
205205
- `Sign`
206-
- `TruncDate`
207206
- `TruncTime`
208207

209208
- The `tzinfo` parameter of the `Trunc` database functions doesn't work

django_mongodb/features.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ def django_test_expected_failures(self):
502502
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func",
503503
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_quarter_func_boundaries",
504504
},
505-
"TruncDate database function not supported.": {
506-
"aggregation.tests.AggregateTestCase.test_aggregation_default_using_date_from_database",
507-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_func",
508-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_date_none",
509-
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_lookup_name_sql_injection",
510-
"expressions.tests.FieldTransformTests.test_multiple_transforms_in_values",
511-
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
505+
"TruncDate comparison against datetime.date() doesn't work": {
506+
# field__date=date() generates a type mismatch:
507+
# {'$eq': [{'$toDate': '$dt'}, datetime.datetime(2014, 3, 12, 0, 0)]}
508+
# since DatabaseOperations.adapt_datefield_value() converts the date.
512509
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
510+
},
511+
"TruncDate database function with timezone not supported.": {
512+
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
513513
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
514514
},
515515
"TruncTime database function not supported.": {

django_mongodb/functions.py

Lines changed: 11 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,15 @@ 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+
if tzname and tzname != "UTC":
200+
raise NotSupportedError(f"TruncDate with timezone ({tzname}) isn't supported on MongoDB.")
201+
return {"$toDate": lhs_mql}
202+
203+
194204
def register_functions():
195205
Cast.as_mql = cast
196206
Concat.as_mql = concat
@@ -212,4 +222,5 @@ def register_functions():
212222
Substr.as_mql = substr
213223
Trim.as_mql = trim("trim")
214224
TruncBase.as_mql = trunc
225+
TruncDate.as_mql = trunc_date
215226
Upper.as_mql = preserve_null("toUpper")

0 commit comments

Comments
 (0)