Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store DateField and TimeField as datetime (rather than string) #40

Merged
merged 2 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions django_mongodb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@
class DatabaseOperations(BaseDatabaseOperations):
compiler_module = "django_mongodb.compiler"

def adapt_datefield_value(self, value):
"""Store DateField as datetime."""
if value is None:
return None
return datetime.datetime.combine(value, datetime.datetime.min.time())

def adapt_datetimefield_value(self, value):
if not settings.USE_TZ and value is not None and timezone.is_naive(value):
value = timezone.make_aware(value)
return value

def adapt_timefield_value(self, value):
"""Store TimeField as datetime."""
if value is None:
return None
return datetime.datetime.combine(datetime.datetime.min.date(), value)

def get_db_converters(self, expression):
converters = super().get_db_converters(expression)
internal_type = expression.output_field.get_internal_type()
Expand All @@ -33,7 +45,7 @@ def get_db_converters(self, expression):

def convert_datefield_value(self, value, expression, connection):
if value is not None:
value = datetime.date.fromisoformat(value)
value = value.date()
return value

def convert_datetimefield_value(self, value, expression, connection):
Expand All @@ -49,7 +61,7 @@ def convert_decimalfield_value(self, value, expression, connection):

def convert_timefield_value(self, value, expression, connection):
if value is not None:
value = datetime.time.fromisoformat(value)
value = value.time()
return value

def convert_uuidfield_value(self, value, expression, connection):
Expand Down