Last active
October 20, 2023 11:39
-
-
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
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
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