@@ -84,65 +84,22 @@ def get_dynamic_params(self):
8484            return  self .parent ._context .get ("dynamic_params" , {})
8585        return  self ._context .get ("dynamic_params" , {})
8686
87-     def  handle_depth_serialization (self , instance , representation ):
88-         """ 
89-         Handles the serialization of fields based on the current depth  
90-         compared to the maximum allowed depth. This function modifies  
91-         the representation to include only URLs for nested serializers  
92-         when the maximum depth is reached. 
93-         """ 
94-         max_depth  =  self ._context .get ("max_depth" , 0 )
95-         current_depth  =  self ._context .get ("current_depth" , 0 )
96- 
97-         # if we reach the maximum depth, nested serializers return their pk (url) 
98-         if  current_depth  >=  max_depth :
99-             for  field_name , field  in  self .fields .items ():
100-                 if  isinstance (field , serializers .HyperlinkedModelSerializer ):
101-                     nested_representation  =  representation .get (field_name )
102-                     if  nested_representation  and  "url"  in  nested_representation :
103-                         representation [field_name ] =  nested_representation ["url" ]
104-             return  representation 
105-         
106-         # otherwise, pass depth to children serializers 
107-         for  field_name , field  in  self .fields .items ():
108-             # Guard clause: make sure the child is a GameContentSerializer 
109-             if  not  isinstance (field , GameContentSerializer ):
110-                 continue 
111- 
112-             nested_instance  =  getattr (instance , field_name )
113-             nested_serializer  =  field .__class__ (nested_instance , context = {
114-                 ** self ._context ,
115-                 "current_depth" : current_depth  +  1 ,
116-                 "max_depth" : max_depth ,
117-             })
118- 
119-             # Ensure dynamic params are specific to the child serializer 
120-             child_dynamic_params  =  self .get_or_create_dynamic_params (field_name )
121-             nested_serializer ._context ['dynamic_params' ] =  child_dynamic_params 
122-             representation [field_name ] =  nested_serializer .data 
123-         return  representation 
124-         
125- 
12687    def  __init__ (self , * args , ** kwargs ):
12788        request  =  kwargs .get ("context" , {}).get ("request" )
12889        super ().__init__ (* args , ** kwargs )
12990
91+         # Request is only present on root serializer 
13092        if  request :
131-             self ._context ["max_depth" ] =  int (request .query_params .get ("depth" , 0 ))
13293            dynamic_params  =  self .get_dynamic_params_for_root (request )
13394            self ._context .update ({"dynamic_params" : dynamic_params })
13495
13596    def  to_representation (self , instance ):
97+         # if dynamic params are present, rmv requested fields and pass params  
98+         # to child serializers 
13699        if  dynamic_params  :=  self .get_dynamic_params ().copy ():
137100            self .remove_unwanted_fields (dynamic_params )
138101            self .set_dynamic_params_for_children (dynamic_params )
139- 
140-         representation  =  super ().to_representation (instance )
141- 
142-         representation  =  self .handle_depth_serialization (instance , representation )
143- 
144-         return  representation 
145- 
102+         return  super ().to_representation (instance )
146103
147104    class  Meta :
148105        abstract  =  True 
0 commit comments