summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvarunnaganathan <varunnaganathan912@gmail.com>2015-12-24 21:12:49 +0530
committerTim Graham <timograham@gmail.com>2016-01-02 08:20:07 -0500
commitf6b4893a9fcc3f9888491820e31c02e074d86341 (patch)
tree11c39ce4f22f57d524e0b68d30875b844b26e1d0
parent1261c49690e25c9887f88b7d7f391764f01e1992 (diff)
[1.8.x] Fixed #25316 -- Fixed a crash with order_by() and values() after annotate().
Backport of 3eba9638ee69138c73efb1d1c1d1b806ddafc6cf from master
-rw-r--r--django/db/models/expressions.py3
-rw-r--r--docs/releases/1.8.8.txt4
-rw-r--r--tests/expressions_case/tests.py12
3 files changed, 18 insertions, 1 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
index 5d6fab6595..83121a9066 100644
--- a/django/db/models/expressions.py
+++ b/django/db/models/expressions.py
@@ -729,7 +729,8 @@ class When(Expression):
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
c = self.copy()
c.is_summary = summarize
- c.condition = c.condition.resolve_expression(query, allow_joins, reuse, summarize, False)
+ if hasattr(c.condition, 'resolve_expression'):
+ c.condition = c.condition.resolve_expression(query, allow_joins, reuse, summarize, False)
c.result = c.result.resolve_expression(query, allow_joins, reuse, summarize, for_save)
return c
diff --git a/docs/releases/1.8.8.txt b/docs/releases/1.8.8.txt
index a888925841..2a8dc5792d 100644
--- a/docs/releases/1.8.8.txt
+++ b/docs/releases/1.8.8.txt
@@ -54,3 +54,7 @@ Bugfixes
* Made ``loaddata`` skip disabling and enabling database constraints when it
doesn't load any fixtures (:ticket:`23372`).
+
+* Fixed a crash in ``QuerySet.values()/values_list()`` after an ``annotate()``
+ and ``order_by()`` when ``values()/values_list()`` includes a field not in
+ the ``order_by()`` (:ticket:`25316`).
diff --git a/tests/expressions_case/tests.py b/tests/expressions_case/tests.py
index d618447833..27aef931c0 100644
--- a/tests/expressions_case/tests.py
+++ b/tests/expressions_case/tests.py
@@ -252,6 +252,18 @@ class CaseExpressionTests(TestCase):
transform=attrgetter('integer', 'test')
)
+ def test_annotate_values_not_in_order_by(self):
+ self.assertEqual(
+ list(CaseTestModel.objects.annotate(test=Case(
+ When(integer=1, then=Value('one')),
+ When(integer=2, then=Value('two')),
+ When(integer=3, then=Value('three')),
+ default=Value('other'),
+ output_field=models.CharField(),
+ )).order_by('test').values_list('integer', flat=True)),
+ [1, 4, 3, 3, 3, 2, 2]
+ )
+
def test_combined_expression(self):
self.assertQuerysetEqual(
CaseTestModel.objects.annotate(