0

I would like to create a serializer that given an input JSON object select certain attributes.

There is an legacy system that sends an object, for example:

{
    "a": {
        "b": "test"
    }
}

I need to keep certain properties of the object. For example a.b. I have created the following serializer, but it is not working:

class CustomSerializer(serializers.Serializer):
    b = serializers.CharField(source='a.b', required=True)

Does anyone know what is failing?

Thanks!

Diego
  • 304
  • 1
  • 8

1 Answers1

0

You need to implement nested serializer. For your example, you can try something like this :

class BSerializer(serializers.Serializer):
    b =  serializers.CharField()

class ASerializer(serializers.Serializer):
    a = BSerializer()

Note : this is just an example of nested serializer. You might need to add few things to make it work properly for you (adding Meta, other fields if needed, use it in view etc. )

Enthusiast Martin
  • 3,041
  • 2
  • 12
  • 20