13
13
from polymorphic .query import convert_to_polymorphic_queryset
14
14
from polymorphic .managers import PolymorphicManager
15
15
from polymorphic .models import PolymorphicTypeInvalid , PolymorphicTypeUndefined
16
+ from polymorphic .query import (
17
+ PolymorphicRelatedQuerySetMixin ,
18
+ )
16
19
from polymorphic .tests .models import (
17
20
AltChildAsBaseModel ,
18
21
AltChildModel ,
@@ -1369,8 +1372,8 @@ def test_select_related_on_poly_classes(self):
1369
1372
obj_list = list (
1370
1373
PlainModel .objects .select_related (
1371
1374
"relation" ,
1372
- "relation__childmodel__link_on_child " ,
1373
- "relation__altchildmodel__link_on_altchild " ,
1375
+ "relation__ChildModel__link_on_child " ,
1376
+ "relation__AltChildModel__link_on_altchild " ,
1374
1377
).order_by ("pk" )
1375
1378
)
1376
1379
with self .assertNumQueries (0 ):
@@ -1387,6 +1390,32 @@ def test_select_related_on_poly_classes(self):
1387
1390
self .assertIsInstance (obj_list [2 ].relation , AltChildModel )
1388
1391
self .assertIsInstance (obj_list [3 ].relation , AltChildModel )
1389
1392
1393
+ def test_select_related_can_merge_fields (self ):
1394
+ # can we fetch the related object but only the minimal 'common' values
1395
+ plain_a_obj_1 = PlainA .objects .create (field1 = "f1" )
1396
+ plain_a_obj_2 = PlainA .objects .create (field1 = "f2" )
1397
+ extra_obj = ModelExtraExternal .objects .create (topic = "t1" )
1398
+ obj_p = ParentModel .objects .create (name = "p1" )
1399
+ obj_c = ChildModel .objects .create (name = "c1" , other_name = "c1name" , link_on_child = extra_obj )
1400
+ obj_ac1 = AltChildModel .objects .create (
1401
+ name = "ac1" , other_name = "ac1name" , link_on_altchild = plain_a_obj_1
1402
+ )
1403
+ obj_ac2 = AltChildModel .objects .create (
1404
+ name = "ac2" , other_name = "ac2name" , link_on_altchild = plain_a_obj_2
1405
+ )
1406
+ obj_p_1 = PlainModel .objects .create (relation = obj_p )
1407
+ obj_p_2 = PlainModel .objects .create (relation = obj_c )
1408
+ obj_p_3 = PlainModel .objects .create (relation = obj_ac1 )
1409
+ obj_p_4 = PlainModel .objects .create (relation = obj_ac2 )
1410
+ ContentType .objects .get_for_models (PlainA , ModelExtraExternal , AltChildModel )
1411
+ base_query = PlainModel .objects .select_related (
1412
+ "relation__ChildModel" ,
1413
+ )
1414
+ base_query = base_query .select_related (
1415
+ "relation__AltChildModel" )
1416
+ with self .assertNumQueries (1 ):
1417
+ list (base_query )
1418
+
1390
1419
def test_select_related_on_poly_classes_simple (self ):
1391
1420
# can we fetch the related object but only the minimal 'common' values
1392
1421
plain_a_obj_1 = PlainA .objects .create (field1 = "f1" )
@@ -1410,13 +1439,13 @@ def test_select_related_on_poly_classes_simple(self):
1410
1439
obj_list = list (
1411
1440
PlainModel .objects .select_related (
1412
1441
"relation" ,
1413
- "relation__childmodel " ,
1414
- "relation__altchildmodel " ,
1442
+ "relation__ChildModel_ " ,
1443
+ "relation__AltChildModel_ " ,
1415
1444
)
1416
1445
.order_by ("pk" )
1417
1446
.only (
1418
1447
"relation__name" ,
1419
- "relation__polymorphic_ctype_id " ,
1448
+ "relation__polymorphic_ctype " ,
1420
1449
)
1421
1450
)
1422
1451
with self .assertNumQueries (0 ):
@@ -1481,8 +1510,8 @@ def test_we_can_upgrade_a_query_set_to_polymorphic(self):
1481
1510
VanillaPlainModel .objects
1482
1511
).select_related (
1483
1512
"relation" ,
1484
- "relation__childmodel " ,
1485
- "relation__altchildmodel " ,
1513
+ "relation__ChildModel " ,
1514
+ "relation__AltChildModel " ,
1486
1515
)
1487
1516
.order_by ("pk" )
1488
1517
)
@@ -1530,8 +1559,8 @@ def test_select_related_on_poly_classes_indirect_related(self):
1530
1559
RefPlainModel .poly_objects .select_related (
1531
1560
# "plainobj__relation",
1532
1561
"plainobj__relation" ,
1533
- "plainobj__relation__childmodel__link_on_child " ,
1534
- "plainobj__relation__altchildmodel__link_on_altchild " ,
1562
+ "plainobj__relation__ChildModel__link_on_child " ,
1563
+ "plainobj__relation__AltChildModel__link_on_altchild " ,
1535
1564
).order_by ("pk" )
1536
1565
)
1537
1566
with self .assertNumQueries (0 ):
@@ -1577,8 +1606,8 @@ def test_select_related_on_poly_classes_indirect_related(self):
1577
1606
RefPlainModel .poly_objects .select_related (
1578
1607
# "plainobj__relation",
1579
1608
"plainobj__relation" ,
1580
- "plainobj__relation__childmodel__link_on_child " ,
1581
- "plainobj__relation__altchildmodel__link_on_altchild " ,
1609
+ "plainobj__relation__ChildModel__link_on_child " ,
1610
+ "plainobj__relation__AltChildModel__link_on_altchild " ,
1582
1611
).order_by ("pk" )
1583
1612
)
1584
1613
with self .assertNumQueries (0 ):
@@ -1669,9 +1698,9 @@ def test_select_related_on_poly_classes_supports_multi_level_inheritance(self):
1669
1698
obj_list = list (
1670
1699
PlainModel .objects .select_related (
1671
1700
"relation" ,
1672
- "relation__childmodel__link_on_child " ,
1673
- "relation__altchildmodel__link_on_altchild " ,
1674
- "relation__altchildmodel__altchildasbasemodel__link_on_altchild " ,
1701
+ "relation__ChildModel__link_on_child " ,
1702
+ "relation__AltChildModel__link_on_altchild " ,
1703
+ "relation__AltChildAsBaseModel__link_on_altchild " ,
1675
1704
).order_by ("pk" )
1676
1705
)
1677
1706
with self .assertNumQueries (0 ):
@@ -1848,7 +1877,7 @@ def test_select_related_field_from_polymorphic_child_class(self):
1848
1877
all_objs = [
1849
1878
obj
1850
1879
for obj in ParentModel .objects .select_related (
1851
- "altchildmodel " ,
1880
+ "AltChildModel " ,
1852
1881
)
1853
1882
]
1854
1883
@@ -1898,7 +1927,8 @@ def test_select_related_field_from_polymorphic_child_class_using_modelnames_mult
1898
1927
# * 0 for AltChildAsBaseModel object as from select_related (x1)
1899
1928
# * 0 for AltChildModel object as part of select_related form
1900
1929
# AltChildAsBaseModel (x1)
1901
- all_objs = [obj for obj in ParentModel .objects .select_related ("AltChildAsBaseModel" )]
1930
+ all_objs = [obj for obj in ParentModel .objects .select_related (
1931
+ "AltChildAsBaseModel" )]
1902
1932
1903
1933
def test_prefetch_object_is_supported (self ):
1904
1934
b1 = RelatingModel .objects .create ()
@@ -1927,7 +1957,7 @@ def test_prefetch_object_is_supported(self):
1927
1957
assert len (objects [0 ].non_poly ) == 1
1928
1958
assert len (objects [1 ].non_poly ) == 1
1929
1959
1930
- def test_select_related_on_poly_classes_preserves_on_relations_annotations (self ):
1960
+ def test_prefetch_related_on_poly_classes_preserves_on_relations_annotations (self ):
1931
1961
b1 = RelatingModel .objects .create ()
1932
1962
b2 = RelatingModel .objects .create ()
1933
1963
b3 = RelatingModel .objects .create ()
0 commit comments