From 3173355f2416caf18c74b542eff8e36bcb3ca173 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 1 Jan 2025 09:56:26 -0500 Subject: [PATCH] fix loss of time component in Value(datetime) --- django_mongodb/expressions.py | 2 ++ tests/expressions_/test_value.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/django_mongodb/expressions.py b/django_mongodb/expressions.py index 15f1448a..5c736b07 100644 --- a/django_mongodb/expressions.py +++ b/django_mongodb/expressions.py @@ -210,6 +210,8 @@ def value(self, compiler, connection): # noqa: ARG001 return {"$literal": value} if isinstance(value, Decimal): return Decimal128(value) + if isinstance(value, datetime.datetime): + return value if isinstance(value, datetime.date): # Turn dates into datetimes since BSON doesn't support dates. return datetime.datetime.combine(value, datetime.datetime.min.time()) diff --git a/tests/expressions_/test_value.py b/tests/expressions_/test_value.py index 51ac2d28..c57c2f03 100644 --- a/tests/expressions_/test_value.py +++ b/tests/expressions_/test_value.py @@ -17,7 +17,7 @@ def test_date(self): def test_datetime(self): self.assertEqual( Value(datetime.datetime(2025, 1, 1, 9, 8, 7)).as_mql(None, None), - datetime.datetime(2025, 1, 1), + datetime.datetime(2025, 1, 1, 9, 8, 7), ) def test_decimal(self):