Skip to content

Commit 6cfb08e

Browse files
committed
store date as datetime
1 parent 263bafc commit 6cfb08e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

django_mongodb/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ def trunc(self, compiler, connection):
4848

4949

5050
def register_functions():
51-
Extract.as_mql = extract
51+
Extract.as_mql = Extract.as_mql_agg = extract
5252
TruncBase.as_mql_agg = trunc

django_mongodb/operations.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
from django.conf import settings
66
from django.db.backends.base.operations import BaseDatabaseOperations
77
from django.utils import timezone
8+
from django.utils.regex_helper import _lazy_re_compile
89

910

1011
class DatabaseOperations(BaseDatabaseOperations):
1112
compiler_module = "django_mongodb.compiler"
1213

14+
def adapt_datefield_value(self, value):
15+
"""
16+
Transform a date value to an object compatible with what is expected
17+
by the backend driver for date columns.
18+
"""
19+
if value is None:
20+
return None
21+
return datetime.datetime.combine(value, datetime.datetime.min.time())
22+
1323
def adapt_datetimefield_value(self, value):
1424
if value is None:
1525
return None
@@ -30,6 +40,29 @@ def adapt_timefield_value(self, value):
3040
raise ValueError("MongoDB backend does not support timezone-aware times.")
3141
return str(value)
3242

43+
# EXTRACT format cannot be passed in parameters.
44+
_extract_format_re = _lazy_re_compile(r"[A-Z_]+")
45+
46+
def date_extract_sql(self, lookup_type, sql, params):
47+
if lookup_type == "week_day":
48+
# For consistency across backends, we return Sunday=1, Saturday=7.
49+
return f"EXTRACT(DOW FROM {sql}) + 1", params
50+
elif lookup_type == "iso_week_day":
51+
return f"EXTRACT(ISODOW FROM {sql})", params
52+
elif lookup_type == "iso_year":
53+
return f"EXTRACT(ISOYEAR FROM {sql})", params
54+
55+
lookup_type = lookup_type.upper()
56+
if not self._extract_format_re.fullmatch(lookup_type):
57+
raise ValueError(f"Invalid lookup type: {lookup_type!r}")
58+
return f"EXTRACT({lookup_type} FROM {sql})", params
59+
60+
def datetime_extract_sql(self, lookup_type, sql, params, tzname):
61+
if lookup_type == "second":
62+
# Truncate fractional seconds.
63+
return f"EXTRACT(SECOND FROM DATE_TRUNC(%s, {sql}))", ("second", *params)
64+
return self.date_extract_sql(lookup_type, sql, params)
65+
3366
def datetime_trunc_sql(self, lookup_type, sql, params, tzname):
3467
return f"DATE_TRUNC(%s, {sql})", (lookup_type, *params)
3568

@@ -54,7 +87,7 @@ def get_db_converters(self, expression):
5487

5588
def convert_datefield_value(self, value, expression, connection):
5689
if value is not None:
57-
value = datetime.date.fromisoformat(value)
90+
value = value.date()
5891
return value
5992

6093
def convert_datetimefield_value(self, value, expression, connection):

0 commit comments

Comments
 (0)