File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,24 @@ class ItemViewSet(viewsets.ReadOnlyModelViewSet):
3636    serializer_class  =  serializers .ItemSerializer 
3737    filterset_class  =  ItemFilterSet 
3838
39+     def  get_queryset (self ):
40+         depth  =  int (self .request .query_params .get ('depth' , 0 ))
41+         queryset  =  ItemViewSet .setup_eager_loading (super ().get_queryset (), self .action , depth )
42+         return  queryset 
43+ 
44+     # Eagerly load nested resources to address N+1 problems 
45+     @staticmethod  
46+     def  setup_eager_loading (queryset , action , depth ):
47+         if  action  ==  'list' :
48+             selects  =  ['armor' , 'weapon' ]
49+             # Prefetch many-to-many and reverse ForeignKey relations 
50+             prefetches  =  [
51+                 'category' , 'document' , 'document__licenses' ,
52+                 'damage_immunities' , 'damage_resistances' , 
53+                 'damage_vulnerabilities' , 'rarity' 
54+             ]
55+             queryset  =  queryset .select_related (* selects ).prefetch_related (* prefetches )
56+         return  queryset 
3957
4058class  ItemRarityViewSet (viewsets .ReadOnlyModelViewSet ):
4159    """ 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments