mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 11:00:13 +03:00
Add Field.many_to_native for more control over iterable serialization
This commit is contained in:
parent
2088023293
commit
86c209cf60
|
@ -160,11 +160,17 @@ class Field(object):
|
||||||
if is_protected_type(value):
|
if is_protected_type(value):
|
||||||
return value
|
return value
|
||||||
elif hasattr(value, '__iter__') and not isinstance(value, (dict, six.string_types)):
|
elif hasattr(value, '__iter__') and not isinstance(value, (dict, six.string_types)):
|
||||||
return [self.to_native(item) for item in value]
|
return self.many_to_native(value)
|
||||||
elif isinstance(value, dict):
|
elif isinstance(value, dict):
|
||||||
return dict(map(self.to_native, (k, v)) for k, v in value.items())
|
return dict(map(self.to_native, (k, v)) for k, v in value.items())
|
||||||
return smart_text(value)
|
return smart_text(value)
|
||||||
|
|
||||||
|
def many_to_native(self, many_values, key_func=(lambda value: value)):
|
||||||
|
"""
|
||||||
|
Return a list of many simple objects.
|
||||||
|
"""
|
||||||
|
return [self.to_native(key_func(value)) for value in many_values]
|
||||||
|
|
||||||
def attributes(self):
|
def attributes(self):
|
||||||
"""
|
"""
|
||||||
Returns a dictionary of attributes to be used when serializing to xml.
|
Returns a dictionary of attributes to be used when serializing to xml.
|
||||||
|
|
|
@ -133,7 +133,7 @@ class RelatedField(WritableField):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if self.many:
|
if self.many:
|
||||||
return [self.to_native(item) for item in value.all()]
|
return self.many_to_native(value.all())
|
||||||
return self.to_native(value)
|
return self.to_native(value)
|
||||||
|
|
||||||
def field_from_native(self, data, files, field_name, into):
|
def field_from_native(self, data, files, field_name, into):
|
||||||
|
@ -223,7 +223,7 @@ class PrimaryKeyRelatedField(RelatedField):
|
||||||
queryset = getattr(obj, self.source or field_name)
|
queryset = getattr(obj, self.source or field_name)
|
||||||
|
|
||||||
# Forward relationship
|
# Forward relationship
|
||||||
return [self.to_native(item.pk) for item in queryset.all()]
|
return self.many_to_native(queryset.all(), lambda item: item.pk)
|
||||||
|
|
||||||
# To-one relationship
|
# To-one relationship
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -317,7 +317,7 @@ class BaseSerializer(Field):
|
||||||
|
|
||||||
# If the object has an "all" method, assume it's a relationship
|
# If the object has an "all" method, assume it's a relationship
|
||||||
if is_simple_callable(getattr(obj, 'all', None)):
|
if is_simple_callable(getattr(obj, 'all', None)):
|
||||||
return [self.to_native(item) for item in obj.all()]
|
return self.many_to_native(obj.all())
|
||||||
|
|
||||||
if obj is None:
|
if obj is None:
|
||||||
return None
|
return None
|
||||||
|
@ -328,7 +328,7 @@ class BaseSerializer(Field):
|
||||||
many = hasattr(obj, '__iter__') and not isinstance(obj, (Page, dict, six.text_type))
|
many = hasattr(obj, '__iter__') and not isinstance(obj, (Page, dict, six.text_type))
|
||||||
|
|
||||||
if many:
|
if many:
|
||||||
return [self.to_native(item) for item in obj]
|
return self.many_to_native(obj)
|
||||||
return self.to_native(obj)
|
return self.to_native(obj)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -385,7 +385,7 @@ class BaseSerializer(Field):
|
||||||
PendingDeprecationWarning, stacklevel=2)
|
PendingDeprecationWarning, stacklevel=2)
|
||||||
|
|
||||||
if many:
|
if many:
|
||||||
self._data = [self.to_native(item) for item in obj]
|
self._data = self.many_to_native(obj)
|
||||||
else:
|
else:
|
||||||
self._data = self.to_native(obj)
|
self._data = self.to_native(obj)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user