mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Pass request to related serializers
Related serializers may need access to the request to properly serialize a child resource. For example, reverse() in djangorestframework.reverse uses request if available to return an absolute URL. While the parent resource has access to the request to generate the absolute URL, the child resource does not.
This commit is contained in:
		
							parent
							
								
									9dbaac3182
								
							
						
					
					
						commit
						87d06697f7
					
				| 
						 | 
					@ -179,7 +179,8 @@ class Serializer(object):
 | 
				
			||||||
            stack = self.stack[:]
 | 
					            stack = self.stack[:]
 | 
				
			||||||
            stack.append(obj)
 | 
					            stack.append(obj)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return related_serializer(depth=depth, stack=stack).serialize(obj)
 | 
					        return related_serializer(depth=depth, stack=stack).serialize(
 | 
				
			||||||
 | 
					            obj, request=self.request)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def serialize_max_depth(self, obj):
 | 
					    def serialize_max_depth(self, obj):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
| 
						 | 
					@ -258,6 +259,10 @@ class Serializer(object):
 | 
				
			||||||
        Convert any object into a serializable representation.
 | 
					        Convert any object into a serializable representation.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Request from related serializer.
 | 
				
			||||||
 | 
					        if request is not None:
 | 
				
			||||||
 | 
					            self.request = request
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if isinstance(obj, (dict, models.Model)):
 | 
					        if isinstance(obj, (dict, models.Model)):
 | 
				
			||||||
            # Model instances & dictionaries
 | 
					            # Model instances & dictionaries
 | 
				
			||||||
            return self.serialize_model(obj)
 | 
					            return self.serialize_model(obj)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user