mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #34226 -- Fixed QuerySet.select_related() with multiple FilteredRelations to the OneToOneField.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user