Fixed #25316 -- Fixed a crash with order_by() and values() after annotate().

This commit is contained in:
varunnaganathan 2015-12-24 21:12:49 +05:30 committed by Tim Graham
parent f0ad641628
commit 3eba9638ee
4 changed files with 22 additions and 1 deletions

View File

@ -758,7 +758,8 @@ class When(Expression):
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False): def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
c = self.copy() c = self.copy()
c.is_summary = summarize 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) c.result = c.result.resolve_expression(query, allow_joins, reuse, summarize, for_save)
return c return c

View File

@ -54,3 +54,7 @@ Bugfixes
* Made ``loaddata`` skip disabling and enabling database constraints when it * Made ``loaddata`` skip disabling and enabling database constraints when it
doesn't load any fixtures (:ticket:`23372`). 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`).

View File

@ -79,3 +79,7 @@ Bugfixes
* Restored ``contrib.auth`` hashers compatibility with py-bcrypt * Restored ``contrib.auth`` hashers compatibility with py-bcrypt
(:ticket:`26016`). (:ticket:`26016`).
* 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`).

View File

@ -252,6 +252,18 @@ class CaseExpressionTests(TestCase):
transform=attrgetter('integer', 'test') 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): def test_combined_expression(self):
self.assertQuerysetEqual( self.assertQuerysetEqual(
CaseTestModel.objects.annotate( CaseTestModel.objects.annotate(