Commit 6f403056 authored by Anssi Kääriäinen's avatar Anssi Kääriäinen Committed by Tim Graham
Browse files

Fixed #24923 -- errored out nicely when using aggregates in order_by()

parent 83f63730
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1643,6 +1643,11 @@ class Query(object):
        for item in ordering:
            if not hasattr(item, 'resolve_expression') and not ORDER_PATTERN.match(item):
                errors.append(item)
            if getattr(item, 'contains_aggregate', False):
                raise FieldError(
                    'Using an aggregate in order_by() without also including '
                    'it in annotate() is not allowed: %s' % item
                )
        if errors:
            raise FieldError('Invalid order_by arguments: %s' % errors)
        if ordering:
+8 −0
Original line number Diff line number Diff line
@@ -105,6 +105,14 @@ class AggregateTestCase(TestCase):
    def test_empty_aggregate(self):
        self.assertEqual(Author.objects.all().aggregate(), {})

    def test_aggregate_in_order_by(self):
        msg = (
            'Using an aggregate in order_by() without also including it in '
            'annotate() is not allowed: Avg(F(book__rating)'
        )
        with self.assertRaisesMessage(FieldError, msg):
            Author.objects.values('age').order_by(Avg('book__rating'))

    def test_single_aggregate(self):
        vals = Author.objects.aggregate(Avg("age"))
        self.assertEqual(vals, {"age__avg": Approximate(37.4, places=1)})