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:
commit
4075aa9674
@ -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
|
||||
|
@ -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"])
|
||||
|
Loading…
Reference in New Issue
Block a user