1313from polymorphic .query import convert_to_polymorphic_queryset
1414from polymorphic .managers import PolymorphicManager
1515from polymorphic .models import PolymorphicTypeInvalid , PolymorphicTypeUndefined
16+ from polymorphic .query import (
17+ PolymorphicRelatedQuerySetMixin ,
18+ )
1619from polymorphic .tests .models import (
1720 AltChildAsBaseModel ,
1821 AltChildModel ,
@@ -1369,8 +1372,8 @@ def test_select_related_on_poly_classes(self):
13691372 obj_list = list (
13701373 PlainModel .objects .select_related (
13711374 "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 " ,
13741377 ).order_by ("pk" )
13751378 )
13761379 with self .assertNumQueries (0 ):
@@ -1387,6 +1390,32 @@ def test_select_related_on_poly_classes(self):
13871390 self .assertIsInstance (obj_list [2 ].relation , AltChildModel )
13881391 self .assertIsInstance (obj_list [3 ].relation , AltChildModel )
13891392
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+
13901419 def test_select_related_on_poly_classes_simple (self ):
13911420 # can we fetch the related object but only the minimal 'common' values
13921421 plain_a_obj_1 = PlainA .objects .create (field1 = "f1" )
@@ -1410,13 +1439,13 @@ def test_select_related_on_poly_classes_simple(self):
14101439 obj_list = list (
14111440 PlainModel .objects .select_related (
14121441 "relation" ,
1413- "relation__childmodel " ,
1414- "relation__altchildmodel " ,
1442+ "relation__ChildModel_ " ,
1443+ "relation__AltChildModel_ " ,
14151444 )
14161445 .order_by ("pk" )
14171446 .only (
14181447 "relation__name" ,
1419- "relation__polymorphic_ctype_id " ,
1448+ "relation__polymorphic_ctype " ,
14201449 )
14211450 )
14221451 with self .assertNumQueries (0 ):
@@ -1481,8 +1510,8 @@ def test_we_can_upgrade_a_query_set_to_polymorphic(self):
14811510 VanillaPlainModel .objects
14821511 ).select_related (
14831512 "relation" ,
1484- "relation__childmodel " ,
1485- "relation__altchildmodel " ,
1513+ "relation__ChildModel " ,
1514+ "relation__AltChildModel " ,
14861515 )
14871516 .order_by ("pk" )
14881517 )
@@ -1530,8 +1559,8 @@ def test_select_related_on_poly_classes_indirect_related(self):
15301559 RefPlainModel .poly_objects .select_related (
15311560 # "plainobj__relation",
15321561 "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 " ,
15351564 ).order_by ("pk" )
15361565 )
15371566 with self .assertNumQueries (0 ):
@@ -1577,8 +1606,8 @@ def test_select_related_on_poly_classes_indirect_related(self):
15771606 RefPlainModel .poly_objects .select_related (
15781607 # "plainobj__relation",
15791608 "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 " ,
15821611 ).order_by ("pk" )
15831612 )
15841613 with self .assertNumQueries (0 ):
@@ -1669,9 +1698,9 @@ def test_select_related_on_poly_classes_supports_multi_level_inheritance(self):
16691698 obj_list = list (
16701699 PlainModel .objects .select_related (
16711700 "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 " ,
16751704 ).order_by ("pk" )
16761705 )
16771706 with self .assertNumQueries (0 ):
@@ -1848,7 +1877,7 @@ def test_select_related_field_from_polymorphic_child_class(self):
18481877 all_objs = [
18491878 obj
18501879 for obj in ParentModel .objects .select_related (
1851- "altchildmodel " ,
1880+ "AltChildModel " ,
18521881 )
18531882 ]
18541883
@@ -1898,7 +1927,8 @@ def test_select_related_field_from_polymorphic_child_class_using_modelnames_mult
18981927 # * 0 for AltChildAsBaseModel object as from select_related (x1)
18991928 # * 0 for AltChildModel object as part of select_related form
19001929 # 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" )]
19021932
19031933 def test_prefetch_object_is_supported (self ):
19041934 b1 = RelatingModel .objects .create ()
@@ -1927,7 +1957,7 @@ def test_prefetch_object_is_supported(self):
19271957 assert len (objects [0 ].non_poly ) == 1
19281958 assert len (objects [1 ].non_poly ) == 1
19291959
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 ):
19311961 b1 = RelatingModel .objects .create ()
19321962 b2 = RelatingModel .objects .create ()
19331963 b3 = RelatingModel .objects .create ()
0 commit comments