summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
author朱穆穆 <mumu.zhu@feat.com>2022-12-26 14:13:44 +0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-27 06:39:38 +0100
commitbbeeb45161da251bb7297b60d6155e7b4efdbc9f (patch)
treed00005532d70b129a129eef9bf465492ca19c0f1 /tests
parent48b6108e50abb1b1f8143721bad8bd7e0be4cc89 (diff)
Fixed #34226 -- Fixed QuerySet.select_related() with multiple FilteredRelations to the OneToOneField.
Diffstat (limited to 'tests')
-rw-r--r--tests/known_related_objects/models.py3
-rw-r--r--tests/known_related_objects/tests.py17
2 files changed, 20 insertions, 0 deletions
diff --git a/tests/known_related_objects/models.py b/tests/known_related_objects/models.py
index bd8fd1d502..027d162828 100644
--- a/tests/known_related_objects/models.py
+++ b/tests/known_related_objects/models.py
@@ -24,3 +24,6 @@ class Pool(models.Model):
class PoolStyle(models.Model):
name = models.CharField(max_length=30)
pool = models.OneToOneField(Pool, models.CASCADE)
+ another_pool = models.OneToOneField(
+ Pool, models.CASCADE, null=True, related_name="another_style"
+ )
diff --git a/tests/known_related_objects/tests.py b/tests/known_related_objects/tests.py
index 0270220061..6080da3838 100644
--- a/tests/known_related_objects/tests.py
+++ b/tests/known_related_objects/tests.py
@@ -1,3 +1,4 @@
+from django.db.models import FilteredRelation
from django.test import TestCase
from .models import Organiser, Pool, PoolStyle, Tournament
@@ -23,6 +24,9 @@ class ExistingRelatedInstancesTests(TestCase):
)
cls.ps1 = PoolStyle.objects.create(name="T1 Pool 2 Style", pool=cls.p2)
cls.ps2 = PoolStyle.objects.create(name="T2 Pool 1 Style", pool=cls.p3)
+ cls.ps3 = PoolStyle.objects.create(
+ name="T1 Pool 1/3 Style", pool=cls.p1, another_pool=cls.p3
+ )
def test_foreign_key(self):
with self.assertNumQueries(2):
@@ -147,3 +151,16 @@ class ExistingRelatedInstancesTests(TestCase):
pools = list(Pool.objects.prefetch_related("poolstyle").order_by("pk"))
self.assertIs(pools[1], pools[1].poolstyle.pool)
self.assertIs(pools[2], pools[2].poolstyle.pool)
+
+ def test_reverse_fk_select_related_multiple(self):
+ with self.assertNumQueries(1):
+ ps = list(
+ PoolStyle.objects.annotate(
+ pool_1=FilteredRelation("pool"),
+ pool_2=FilteredRelation("another_pool"),
+ )
+ .select_related("pool_1", "pool_2")
+ .order_by("-pk")
+ )
+ self.assertIs(ps[0], ps[0].pool_1.poolstyle)
+ self.assertIs(ps[0], ps[0].pool_2.another_style)