Skip to content

Commit 04ecfa0

Browse files
committed
make Value encode date as datetime
1 parent a947bd2 commit 04ecfa0

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

django_mongodb/expressions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from decimal import Decimal
23

34
from bson import Decimal128
@@ -66,6 +67,9 @@ def value(self, compiler, connection): # noqa: ARG001
6667
value = self.value
6768
if isinstance(value, Decimal):
6869
value = Decimal128(value)
70+
elif isinstance(value, datetime.date):
71+
# Turn dates into datetimes since BSON doesn't support dates.
72+
value = datetime.datetime.combine(value, datetime.datetime.min.time())
6973
return {"$literal": value}
7074

7175

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6161
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_and_alias_filter_in_subquery",
6262
# Length of null considered zero rather than null.
6363
"db_functions.text.test_length.LengthTests.test_basic",
64-
# Case(..., then=datetime.date()) crashes: bson.errors.InvalidDocument:
65-
# cannot encode object: datetime.date(2024, 5, 14), of type: <class 'datetime.date'>
66-
"expressions_case.tests.CaseDocumentationExamples.test_filter_example",
6764
}
6865
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
6966
_django_test_expected_failures_bitwise = {

0 commit comments

Comments
 (0)