Commit 286d0e6a authored by Andriy Sokolovskiy's avatar Andriy Sokolovskiy Committed by Tim Graham
Browse files

Refs #24833 -- Forwardported some of "Fixed Case expressions with exclude()."

Partial forwardport of 469f1e36 from stable/1.8.x
as the issue was already fixed in master.
parent cbe4efcb
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -1026,9 +1026,9 @@ class Query(object):
        """
        lookup_splitted = lookup.split(LOOKUP_SEP)
        if self._annotations:
            aggregate, aggregate_lookups = refs_expression(lookup_splitted, self.annotations)
            if aggregate:
                return aggregate_lookups, (), aggregate
            expression, expression_lookups = refs_expression(lookup_splitted, self.annotations)
            if expression:
                return expression_lookups, (), expression
        _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
        field_parts = lookup_splitted[0:len(lookup_splitted) - len(lookup_parts)]
        if len(lookup_parts) == 0:
@@ -1145,7 +1145,7 @@ class Query(object):
        arg, value = filter_expr
        if not 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:
            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)

        clause = self.where_class()
        if reffed_aggregate:
            condition = self.build_lookup(lookups, reffed_aggregate, value)
        if reffed_expression:
            condition = self.build_lookup(lookups, reffed_expression, value)
            clause.add(condition, AND)
            return clause, []

+3 −0
Original line number Diff line number Diff line
@@ -50,3 +50,6 @@ Bugfixes

* Fixed recording of applied status for squashed (replacement) migrations
  (:ticket:`24628`).

* Fixed queryset annotations when using ``Case`` expressions with ``exclude()``
  (:ticket:`24833`).
+12 −0
Original line number Diff line number Diff line
@@ -240,6 +240,18 @@ class CaseExpressionTests(TestCase):
            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):
        self.assertQuerysetEqual(
            CaseTestModel.objects.annotate(