diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py index 279297ac85..27fe486e2f 100644 --- a/django/db/backends/base/operations.py +++ b/django/db/backends/base/operations.py @@ -505,6 +505,10 @@ class BaseDatabaseOperations: """ if value is None: return None + # Expression values are adapted by the database. + if hasattr(value, 'resolve_expression'): + return value + return str(value) def adapt_timefield_value(self, value): @@ -514,6 +518,10 @@ class BaseDatabaseOperations: """ 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("Django does not support timezone-aware times.") return str(value) diff --git a/tests/backends/base/test_operations.py b/tests/backends/base/test_operations.py index b7b7b9e3fc..535cb20f41 100644 --- a/tests/backends/base/test_operations.py +++ b/tests/backends/base/test_operations.py @@ -3,7 +3,7 @@ import decimal from django.core.management.color import no_style from django.db import NotSupportedError, connection, transaction from django.db.backends.base.operations import BaseDatabaseOperations -from django.db.models import DurationField +from django.db.models import DurationField, Value from django.test import ( SimpleTestCase, TestCase, TransactionTestCase, override_settings, skipIfDBFeature, @@ -72,9 +72,17 @@ class SimpleDatabaseOperationTests(SimpleTestCase): def test_adapt_timefield_value_none(self): self.assertIsNone(self.ops.adapt_timefield_value(None)) - def test_adapt_datetimefield_value(self): + def test_adapt_timefield_value_expression(self): + value = Value(timezone.now().time()) + self.assertEqual(self.ops.adapt_timefield_value(value), value) + + def test_adapt_datetimefield_value_none(self): self.assertIsNone(self.ops.adapt_datetimefield_value(None)) + def test_adapt_datetimefield_value_expression(self): + value = Value(timezone.now()) + self.assertEqual(self.ops.adapt_datetimefield_value(value), value) + def test_adapt_timefield_value(self): msg = 'Django does not support timezone-aware times.' with self.assertRaisesMessage(ValueError, msg):