1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #34013 -- Added QuerySet.order_by() support for annotation transforms.

Thanks Eugene Morozov and Ben Nace for the reports.
This commit is contained in:
Simon Charette
2023-12-08 02:03:14 -05:00
committed by Mariusz Felisiak
parent fcf95e5927
commit b0ad41198b
6 changed files with 97 additions and 12 deletions

View File

@@ -1,7 +1,16 @@
import operator
from django.db import DatabaseError, NotSupportedError, connection
from django.db.models import Exists, F, IntegerField, OuterRef, Subquery, Value
from django.db.models import (
Exists,
F,
IntegerField,
OuterRef,
Subquery,
Transform,
Value,
)
from django.db.models.functions import Mod
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
from django.test.utils import CaptureQueriesContext
@@ -322,6 +331,23 @@ class QuerySetSetOperationTests(TestCase):
operator.itemgetter("num"),
)
def test_order_by_annotation_transform(self):
class Mod2(Mod, Transform):
def __init__(self, expr):
super().__init__(expr, 2)
output_field = IntegerField()
output_field.register_instance_lookup(Mod2, "mod2")
qs1 = Number.objects.annotate(
annotation=Value(1, output_field=output_field),
)
qs2 = Number.objects.annotate(
annotation=Value(2, output_field=output_field),
)
msg = "Ordering combined queries by transforms is not implemented."
with self.assertRaisesMessage(NotImplementedError, msg):
list(qs1.union(qs2).order_by("annotation__mod2"))
def test_union_with_select_related_and_order(self):
e1 = ExtraInfo.objects.create(value=7, info="e1")
a1 = Author.objects.create(name="a1", num=1, extra=e1)