mirror of https://github.com/django/django.git
Refs #24833 -- Forwardported some of "Fixed Case expressions with exclude()."
Partial forwardport of 469f1e362b
from stable/1.8.x
as the issue was already fixed in master.
This commit is contained in:
parent
cbe4efcbc1
commit
286d0e6ab1
|
@ -1026,9 +1026,9 @@ class Query(object):
|
||||||
"""
|
"""
|
||||||
lookup_splitted = lookup.split(LOOKUP_SEP)
|
lookup_splitted = lookup.split(LOOKUP_SEP)
|
||||||
if self._annotations:
|
if self._annotations:
|
||||||
aggregate, aggregate_lookups = refs_expression(lookup_splitted, self.annotations)
|
expression, expression_lookups = refs_expression(lookup_splitted, self.annotations)
|
||||||
if aggregate:
|
if expression:
|
||||||
return aggregate_lookups, (), aggregate
|
return expression_lookups, (), expression
|
||||||
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
|
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
|
||||||
field_parts = lookup_splitted[0:len(lookup_splitted) - len(lookup_parts)]
|
field_parts = lookup_splitted[0:len(lookup_splitted) - len(lookup_parts)]
|
||||||
if len(lookup_parts) == 0:
|
if len(lookup_parts) == 0:
|
||||||
|
@ -1145,7 +1145,7 @@ class Query(object):
|
||||||
arg, value = filter_expr
|
arg, value = filter_expr
|
||||||
if not arg:
|
if not arg:
|
||||||
raise FieldError("Cannot parse keyword query %r" % arg)
|
raise FieldError("Cannot parse keyword query %r" % arg)
|
||||||
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
|
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
|
||||||
if not allow_joins and len(parts) > 1:
|
if not allow_joins and len(parts) > 1:
|
||||||
raise FieldError("Joined field references are not permitted in this query")
|
raise FieldError("Joined field references are not permitted in this query")
|
||||||
|
|
||||||
|
@ -1154,8 +1154,8 @@ class Query(object):
|
||||||
value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
|
value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
|
||||||
|
|
||||||
clause = self.where_class()
|
clause = self.where_class()
|
||||||
if reffed_aggregate:
|
if reffed_expression:
|
||||||
condition = self.build_lookup(lookups, reffed_aggregate, value)
|
condition = self.build_lookup(lookups, reffed_expression, value)
|
||||||
clause.add(condition, AND)
|
clause.add(condition, AND)
|
||||||
return clause, []
|
return clause, []
|
||||||
|
|
||||||
|
|
|
@ -50,3 +50,6 @@ Bugfixes
|
||||||
|
|
||||||
* Fixed recording of applied status for squashed (replacement) migrations
|
* Fixed recording of applied status for squashed (replacement) migrations
|
||||||
(:ticket:`24628`).
|
(:ticket:`24628`).
|
||||||
|
|
||||||
|
* Fixed queryset annotations when using ``Case`` expressions with ``exclude()``
|
||||||
|
(:ticket:`24833`).
|
||||||
|
|
|
@ -240,6 +240,18 @@ class CaseExpressionTests(TestCase):
|
||||||
transform=itemgetter('integer', 'max', 'test')
|
transform=itemgetter('integer', 'max', 'test')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_annotate_exclude(self):
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
CaseTestModel.objects.annotate(test=Case(
|
||||||
|
When(integer=1, then=Value('one')),
|
||||||
|
When(integer=2, then=Value('two')),
|
||||||
|
default=Value('other'),
|
||||||
|
output_field=models.CharField(),
|
||||||
|
)).exclude(test='other').order_by('pk'),
|
||||||
|
[(1, 'one'), (2, 'two'), (2, 'two')],
|
||||||
|
transform=attrgetter('integer', 'test')
|
||||||
|
)
|
||||||
|
|
||||||
def test_combined_expression(self):
|
def test_combined_expression(self):
|
||||||
self.assertQuerysetEqual(
|
self.assertQuerysetEqual(
|
||||||
CaseTestModel.objects.annotate(
|
CaseTestModel.objects.annotate(
|
||||||
|
|
Loading…
Reference in New Issue