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

Fixed #36210, Refs #36181 -- Allowed Subquery usage in further lookups against composite pks.

Follow-up to 8561100425.

Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
Jacob Walls
2025-05-11 22:04:09 -04:00
committed by Sarah Boyce
parent de7bb7eab8
commit fd569dd45b
7 changed files with 101 additions and 6 deletions

View File

@@ -988,11 +988,24 @@ class BasicExpressionsTests(TestCase):
)
.order_by("-salary_raise")
.values("salary_raise")[:1],
output_field=IntegerField(),
),
).get(pk=self.gmbh.pk)
self.assertEqual(gmbh_salary.max_ceo_salary_raise, 2332)
def test_annotation_with_outerref_and_output_field(self):
gmbh_salary = Company.objects.annotate(
max_ceo_salary_raise=Subquery(
Company.objects.annotate(
salary_raise=OuterRef("num_employees") + F("num_employees"),
)
.order_by("-salary_raise")
.values("salary_raise")[:1],
output_field=DecimalField(),
),
).get(pk=self.gmbh.pk)
self.assertEqual(gmbh_salary.max_ceo_salary_raise, 2332.0)
self.assertIsInstance(gmbh_salary.max_ceo_salary_raise, Decimal)
def test_annotation_with_nested_outerref(self):
self.gmbh.point_of_contact = Employee.objects.get(lastname="Meyer")
self.gmbh.save()
@@ -2542,6 +2555,15 @@ class ExistsTests(TestCase):
self.assertSequenceEqual(qs, [manager])
self.assertIs(qs.get().exists, False)
def test_annotate_by_empty_custom_exists(self):
class CustomExists(Exists):
template = Subquery.template
manager = Manager.objects.create()
qs = Manager.objects.annotate(exists=CustomExists(Manager.objects.none()))
self.assertSequenceEqual(qs, [manager])
self.assertIs(qs.get().exists, False)
class FieldTransformTests(TestCase):
@classmethod