mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-10-30 23:47:53 +03:00 
			
		
		
		
	Merge pull request #181 from flashingpumpkin/master
Maintain a reference to the parent/root serializer
This commit is contained in:
		
						commit
						db7d15d5d1
					
				|  | @ -169,8 +169,9 @@ class FormResource(Resource): | |||
|             ) | ||||
| 
 | ||||
|             # Add any unknown field errors | ||||
|             for key in unknown_fields: | ||||
|                 field_errors[key] = [u'This field does not exist.'] | ||||
|             if not self.allow_unknown_form_fields: | ||||
|                 for key in unknown_fields: | ||||
|                     field_errors[key] = [u'This field does not exist.'] | ||||
| 
 | ||||
|             if field_errors: | ||||
|                 detail[u'field_errors'] = field_errors | ||||
|  |  | |||
|  | @ -97,6 +97,11 @@ class Serializer(object): | |||
|     The maximum depth to serialize to, or `None`. | ||||
|     """ | ||||
|      | ||||
|     parent = None | ||||
|     """ | ||||
|     A reference to the root serializer when descending down into fields. | ||||
|     """ | ||||
| 
 | ||||
|     def __init__(self, depth=None, stack=[], **kwargs): | ||||
|         if depth is not None: | ||||
|             self.depth = depth | ||||
|  | @ -130,9 +135,12 @@ class Serializer(object): | |||
|         # If an element in `fields` is a 2-tuple of (str, tuple) | ||||
|         # then the second element of the tuple is the fields to | ||||
|         # set on the related serializer | ||||
| 
 | ||||
|         class OnTheFlySerializer(self.__class__): | ||||
|             fields = info | ||||
|             parent = getattr(self, 'parent') or self | ||||
| 
 | ||||
|         if isinstance(info, (list, tuple)): | ||||
|             class OnTheFlySerializer(self.__class__): | ||||
|                 fields = info | ||||
|             return OnTheFlySerializer | ||||
| 
 | ||||
|         # If an element in `fields` is a 2-tuple of (str, Serializer) | ||||
|  | @ -150,8 +158,9 @@ class Serializer(object): | |||
|         elif isinstance(info, str) and info in _serializers: | ||||
|             return _serializers[info] | ||||
| 
 | ||||
|         # Otherwise use `related_serializer` or fall back to `Serializer` | ||||
|         return getattr(self, 'related_serializer') or Serializer | ||||
|         # Otherwise use `related_serializer` or fall back to | ||||
|         # `OnTheFlySerializer` preserve custom serialization methods. | ||||
|         return getattr(self, 'related_serializer') or OnTheFlySerializer | ||||
| 
 | ||||
|     def serialize_key(self, key): | ||||
|         """ | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user