Skip to content

fix F expression with datetime/time lookups #71

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

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,12 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"lookup.tests.LookupTests.test_lookup_collision",
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression",
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression_flat",
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
"expressions_case.tests.CaseExpressionTests.test_join_promotion",
"expressions_case.tests.CaseExpressionTests.test_join_promotion_multiple_annotations",
"ordering.tests.OrderingTests.test_order_by_grandparent_fk_with_expression_in_default_ordering",
"ordering.tests.OrderingTests.test_order_by_parent_fk_with_expression_in_default_ordering",
"ordering.tests.OrderingTests.test_order_by_ptr_field_with_default_ordering_by_expression",
# 'Col' object has no attribute 'utcoffset'
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
"expressions.tests.IterableLookupInnerExpressionsTests.test_in_lookup_allows_F_expressions_and_expressions_for_datetimes",
# pymongo.errors.OperationFailure: $multiply only supports numeric
# types, not date. (should be wrapped in DatabaseError).
"expressions.tests.FTimeDeltaTests.test_invalid_operator",
Expand Down
6 changes: 6 additions & 0 deletions django_mongodb/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def adapt_datefield_value(self, value):
def adapt_datetimefield_value(self, value):
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, "resolve_expression"):
return value
if timezone.is_aware(value):
if settings.USE_TZ:
value = timezone.make_naive(value, self.connection.timezone)
Expand Down Expand Up @@ -64,6 +67,9 @@ def adapt_timefield_value(self, value):
"""Store TimeField as datetime."""
if value is None:
return None
# Expression values are adapted by the database.
if hasattr(value, "resolve_expression"):
return value
if timezone.is_aware(value):
raise ValueError("MongoDB backend does not support timezone-aware times.")
return datetime.datetime.combine(datetime.datetime.min.date(), value)
Expand Down