From ff2aa4019259947734c4791d6afc9f5405d901d0 Mon Sep 17 00:00:00 2001 From: Josh Smeaton Date: Mon, 16 Mar 2015 15:51:23 +1100 Subject: [PATCH] Fixed #24486 -- Fixed error with datetime and DurationField arithmetic --- django/db/models/expressions.py | 7 ++++--- tests/expressions/tests.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 41ce6ba187..0e328ccf55 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -398,9 +398,10 @@ class DurationExpression(Expression): output = side.output_field except FieldError: pass - if output.get_internal_type() == 'DurationField': - sql, params = compiler.compile(side) - return connection.ops.format_for_duration_arithmetic(sql), params + else: + if output.get_internal_type() == 'DurationField': + sql, params = compiler.compile(side) + return connection.ops.format_for_duration_arithmetic(sql), params return compiler.compile(side) def as_sql(self, compiler, connection): diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 59fd063103..6297d68095 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -617,8 +617,8 @@ class ExpressionOperatorTests(TestCase): class FTimeDeltaTests(TestCase): def setUp(self): - sday = datetime.date(2010, 6, 25) - stime = datetime.datetime(2010, 6, 25, 12, 15, 30, 747000) + self.sday = sday = datetime.date(2010, 6, 25) + self.stime = stime = datetime.datetime(2010, 6, 25, 12, 15, 30, 747000) midnight = datetime.time(0) delta0 = datetime.timedelta(0) @@ -821,6 +821,15 @@ class FTimeDeltaTests(TestCase): Experiment.objects.filter(estimated_time__lt=F('end') - F('start'))] self.assertEqual(over_estimate, ['e4']) + def test_duration_with_datetime(self): + # Exclude e1 which has very high precision so we can test this on all + # backends regardless of whether or not it supports + # microsecond_precision. + over_estimate = Experiment.objects.exclude(name='e1').filter( + completed__gt=self.stime + F('estimated_time'), + ).order_by('name') + self.assertQuerysetEqual(over_estimate, ['e3', 'e4'], lambda e: e.name) + class ValueTests(TestCase): def test_update_TimeField_using_Value(self):