Last active
October 13, 2016 05:11
-
-
Save estebistec/7775918 to your computer and use it in GitHub Desktop.
Django-REST-framework list serializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nested_from_native(nested_field, data): | |
if isinstance(nested_field, serializers.BaseSerializer): | |
return nested_field.from_native(data, None) | |
return nested_field.from_native(data) | |
class ListField(fields.WritableField): | |
def __init__(self, item_field, *args, **kwargs): | |
super(ListField, self).__init__(*args, **kwargs) | |
self.item_field = item_field | |
def to_native(self, obj): | |
if obj: | |
return [ | |
self.item_field.to_native(item) | |
for item in obj | |
] | |
def from_native(self, data): | |
if data: | |
return [ | |
nested_from_native(self.item_field, item_data) | |
for item_data in data | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment