1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Merge branch 'fix_outerref_orderby' of https://github.com/AyushKhatri-Dev/django into fix_outerref_orderby

This commit is contained in:
Ayush khatri 2024-12-15 17:44:04 +05:30
commit 4075aa9674
2 changed files with 6 additions and 36 deletions

View File

@ -123,9 +123,10 @@ class JSONFieldModel(models.Model):
class Meta:
required_db_features = {"supports_json_field"}
class NamedCategory(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
def __str__(self):
return self.name
return self.name

View File

@ -6,7 +6,6 @@ from collections import namedtuple
from copy import deepcopy
from decimal import Decimal
from unittest import mock
from .models import NamedCategory
from django.core.exceptions import FieldError
from django.db import DatabaseError, NotSupportedError, connection
@ -82,6 +81,7 @@ from .models import (
Employee,
Experiment,
Manager,
NamedCategory,
Number,
RemoteEmployee,
Result,
@ -2852,38 +2852,6 @@ class OrderByTests(SimpleTestCase):
F("field").desc(nulls_last=False)
class OuterRefOrderByTestCase(TestCase):
@classmethod
def setUpTestData(cls):
cls.ceo = Employee.objects.create(
firstname="John", lastname="Doe", salary=100000
)
cls.gmbh = Company.objects.create(
name="GmbH",
num_employees=50,
ceo=cls.ceo,
point_of_contact=cls.ceo,
based_in_eu=True,
)
cls.foobar_ltd = Company.objects.create(
name="Foobar Ltd",
num_employees=100,
ceo=cls.ceo,
point_of_contact=cls.ceo,
based_in_eu=False,
)
def test_order_by_with_outerref(self):
inner = Company.objects.filter(
num_employees__lt=OuterRef("num_employees")
).order_by("num_employees")
qs = Company.objects.annotate(next_bigger=Subquery(inner.values("pk")[:1]))
foobar_ltd = qs.get(pk=self.foobar_ltd.pk)
self.assertIsNotNone(foobar_ltd.next_bigger)
self.assertEqual(foobar_ltd.next_bigger, self.gmbh.pk)
class SubqueryTests(TestCase):
def test_outer_ref_order_by(self):
NamedCategory.objects.create(id=1, name="first")
@ -2893,14 +2861,15 @@ class SubqueryTests(TestCase):
outer_query = NamedCategory.objects.all()
subquery = (
NamedCategory.objects.filter(id=OuterRef("id"))
.order_by("name")
NamedCategory.objects.filter(pk=OuterRef("pk"))
.order_by("pk")
.values("name")
)
values = outer_query.annotate(sorted_name=Subquery(subquery)).order_by(
"sorted_name"
)
sorted_names = list(values.values_list("sorted_name", flat=True))
self.assertListEqual(sorted_names, ["first", "fourth", "second", "third"])