diff options
| author | Josh Smeaton <josh.smeaton@gmail.com> | 2015-03-16 15:51:23 +1100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-03-17 08:36:15 -0400 |
| commit | 34e09e532da41d56d3ba68babd05c4fd848abaa2 (patch) | |
| tree | cf85977b562305e8ab488cb54b5541104d3b5447 | |
| parent | e150bbd4d65b4b2c3897bdabdda94eba508747a2 (diff) | |
[1.8.x] Fixed #24486 -- Fixed error with datetime and DurationField arithmetic
Backport of ff2aa4019259947734c4791d6afc9f5405d901d0 from master
| -rw-r--r-- | django/db/models/expressions.py | 7 | ||||
| -rw-r--r-- | 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): |
