Skip to content

Instantly share code, notes, and snippets.

@EnriqueSoria
Last active October 20, 2023 11:39
Show Gist options
  • Save EnriqueSoria/eaffaae1687db592e3f9f60baed3bd11 to your computer and use it in GitHub Desktop.
Save EnriqueSoria/eaffaae1687db592e3f9f60baed3bd11 to your computer and use it in GitHub Desktop.
Django Rest Framework list serializer that allows you to specify a "source" field in the child serializer
from rest_framework import serializers
from rest_framework.fields import get_attribute
class ChildSourceListSerializer(serializers.ListSerializer):
"""List serializer that respects child's `source` field"""
def get_attribute(self, instance):
attribute = super().get_attribute(instance)
return [get_attribute(item, self.child.source_attrs) for item in attribute]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment