Skip to content

Instantly share code, notes, and snippets.

@estebistec
Last active October 13, 2016 05:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estebistec/7775918 to your computer and use it in GitHub Desktop.
Save estebistec/7775918 to your computer and use it in GitHub Desktop.
Django-REST-framework list serializer
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