[Django]-Nesting fields in serialization with Django REST framework

3👍

You can try like this:

class CaseSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField('client')

    class Meta:
        model = Case
        fields = ('opened', 'client')

    def client(self, obj):
        client_fields = {}
        client_fields['first_name'] = obj.client_first_name
        client_fields['last_name'] = obj.client_last_name
        return client_fields
👤cezar

Leave a comment