@@ -349,8 +349,9 @@ def for_serialization(self):
349349 Prefetch (
350350 "topics" ,
351351 queryset = LearningResourceTopic .objects .for_serialization (),
352+ to_attr = "_topics" ,
352353 ),
353- Prefetch ("resource_prices" ),
354+ Prefetch ("resource_prices" , to_attr = "_resource_prices" ),
354355 Prefetch (
355356 "offered_by" ,
356357 queryset = LearningResourceOfferor .objects .for_serialization (),
@@ -386,6 +387,7 @@ def for_serialization(self):
386387 Prefetch (
387388 "children" ,
388389 queryset = LearningResourceRelationship .objects .for_serialization (),
390+ to_attr = "_children" ,
389391 ),
390392 Prefetch (
391393 "direct_content_files" ,
@@ -440,6 +442,7 @@ def for_serialization(self):
440442 ).select_related ("school" ),
441443 ),
442444 ),
445+ to_attr = "_direct_content_files" ,
443446 ),
444447 * LearningResourceDetailModel .get_subclass_prefetches (),
445448 ).select_related ("image" , "platform" )
@@ -642,39 +645,37 @@ def published_runs(self) -> list["LearningResourceRun"]:
642645
643646 def topics_for_serialization (self ):
644647 """Return topics using the prefetch cache when available."""
645- prefetched_topics = getattr (self , "_prefetched_objects_cache" , {}).get ("topics" )
646- if prefetched_topics is not None :
647- return list (prefetched_topics )
648+ if hasattr (self , "_topics" ):
649+ return self ._topics
648650 return list (self .topics .for_serialization ())
649651
652+ def resource_prices_for_serialization (self ):
653+ """Return resource prices using the prefetch cache when available."""
654+ if hasattr (self , "_resource_prices" ):
655+ return self ._resource_prices
656+ return list (self .resource_prices .all ())
657+
650658 def resource_price_amounts_for_serialization (self ):
651659 """Return resource price amounts using the prefetch cache when available."""
652- prefetched_prices = getattr (self , "_prefetched_objects_cache" , {}).get (
653- "resource_prices"
654- )
655- prices = (
656- prefetched_prices
657- if prefetched_prices is not None
658- else self .resource_prices .all ()
660+ return [price .amount for price in self .resource_prices_for_serialization ()]
661+
662+ def children_for_serialization (self ):
663+ """Return children using the prefetch cache when available."""
664+ if hasattr (self , "_children" ):
665+ return self ._children
666+ return list (
667+ self .children .order_by ("position" ).select_related ("child" , "child__image" )
659668 )
660- return [price .amount for price in prices ]
661669
662670 def first_child_relationship_for_serialization (self ):
663671 """Return the first child relationship ordered by position."""
664- prefetched_children = getattr (self , "_prefetched_objects_cache" , {}).get (
665- "children"
666- )
667- if prefetched_children is not None :
668- return prefetched_children [0 ] if prefetched_children else None
669- return self .children .order_by ("position" ).first ()
672+ children = self .children_for_serialization ()
673+ return children [0 ] if children else None
670674
671675 def direct_content_files_for_serialization (self ):
672676 """Return direct content files using the prefetch cache when available."""
673- prefetched_content_files = getattr (self , "_prefetched_objects_cache" , {}).get (
674- "direct_content_files"
675- )
676- if prefetched_content_files is not None :
677- return list (prefetched_content_files )
677+ if hasattr (self , "_direct_content_files" ):
678+ return self ._direct_content_files
678679 return list (self .direct_content_files .for_serialization ())
679680
680681 @cached_property
@@ -1257,19 +1258,16 @@ class UserList(TimestampedModel):
12571258
12581259 def topics_for_serialization (self ):
12591260 """Return topics using the prefetch cache when available."""
1260- prefetched_topics = getattr (self , "_prefetched_objects_cache" , {}).get ("topics" )
1261- if prefetched_topics is not None :
1262- return list (prefetched_topics )
1261+ if hasattr (self , "_topics" ):
1262+ return self ._topics
12631263 return list (self .topics .for_serialization ())
12641264
12651265 def first_child_relationship_for_serialization (self ):
12661266 """Return the first child relationship ordered by position."""
1267- prefetched_children = getattr (self , "_prefetched_objects_cache" , {}).get (
1268- "children"
1269- )
1270- if prefetched_children is not None :
1271- return prefetched_children [0 ] if prefetched_children else None
1272- return self .children .order_by ("position" ).first ()
1267+ children = getattr (self , "_children" , None )
1268+ if children is None :
1269+ return self .children .order_by ("position" ).first ()
1270+ return children [0 ] if children else None
12731271
12741272
12751273class UserListQuerySet (TimestampedModelQuerySet ):
@@ -1281,12 +1279,14 @@ def for_serialization(self):
12811279 Prefetch (
12821280 "topics" ,
12831281 queryset = LearningResourceTopic .objects .for_serialization (),
1282+ to_attr = "_topics" ,
12841283 ),
12851284 Prefetch (
12861285 "children" ,
12871286 queryset = UserListRelationship .objects .select_related (
12881287 "child" , "child__image"
12891288 ).order_by ("position" ),
1289+ to_attr = "_children" ,
12901290 ),
12911291 )
12921292
0 commit comments