Commit 5cda1d27 authored by Alexey Voronov's avatar Alexey Voronov Committed by Tim Graham
Browse files

[1.6.x] Fixed #21643 -- repeated execution of qs with F() + timedelta

Thanks Tim Graham for review and Tai Lee for the additional test to prove
this was a regression in 1.6.

Backport of 7f2485b4 and 81372159 from master
parent f2b513c9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ class SQLEvaluator(object):
    def evaluate_date_modifier_node(self, node, qn, connection):
        timedelta = node.children.pop()
        sql, params = self.evaluate_node(node, qn, connection)
        node.children.append(timedelta)

        if timedelta.days == 0 and timedelta.seconds == 0 and \
                timedelta.microseconds == 0:
+4 −0
Original line number Diff line number Diff line
@@ -14,3 +14,7 @@ several bugs in 1.6.2:
* Fixed ``AttributeError`` when using
  :meth:`~django.db.models.query.QuerySet.bulk_create` with ``ForeignObject``
  (`#21566 <http://code.djangoproject.com/ticket/21566>`_).

* Fixed crash of ``QuerySet``\s that use ``F() + timedelta()`` when their query
  was compiled more once
  (`#21643 <http://code.djangoproject.com/ticket/21643>`_).
+14 −0
Original line number Diff line number Diff line
@@ -259,6 +259,20 @@ class FTimeDeltaTests(TestCase):
        self.days_long.append(e4.completed-e4.assigned)
        self.expnames = [e.name for e in Experiment.objects.all()]

    def test_multiple_query_compilation(self):
        # Ticket #21643
        queryset = Experiment.objects.filter(end__lt=F('start') + datetime.timedelta(hours=1))
        q1 = str(queryset.query)
        q2 = str(queryset.query)
        self.assertEqual(q1, q2)

    def test_query_clone(self):
        # Ticket #21643
        qs = Experiment.objects.filter(end__lt=F('start') + datetime.timedelta(hours=1))
        qs2 = qs.all()
        list(qs)
        list(qs2)

    def test_delta_add(self):
        for i in range(len(self.deltas)):
            delta = self.deltas[i]