title: How choose the right serializer depends on specific field ? e.g. type
body: <p>I'd like to serialize an input from an API to check if the format is valid</p>
  <pre><code>ProfileSerializer(payload).is_valid()
  </code></pre>
  <p><code>profile.py</code></p>
  <pre><code>from rest_framework import serializers
  
  from .node import NodeSerializer
  
  class ProfileSerializer(serializers.Serializer):
      node = NodeSerializer(many=True)
  </code></pre>
  <p><code>node.py</code></p>
  <pre><code>from rest_framework import serializers
  
  from .point import PointSerializer
  
  class NodeSerializer(serializers.Serializer):
      id = serializers.IntegerField()
      points = PointSerializer(many=True)
      type = serializers.CharField(max_length=50)
  </code></pre>
  <p>The issue is the <code>node</code> format, depend on the <code>type</code> -&gt; <code>SEGMENT, ARC, ...</code> I need to select the right serializer.</p>
  <p>So I create a serializer for each type <code>type</code> -&gt; <code>SEGMENT, ARC, ...</code> but I can't figure out how to use them inside the <code>ProfileSerializer</code>.</p>
  <p>How can I catch the type field and use it inside a provider parser function</p>
  <p>I'd like to make something like this:</p>
  <p><code>node_serializer_provider.py</code></p>
  <pre><code>from commons.interfaces.object_factory import ObjectFactory
  from enum import Enum
  from central.serializers.fao.node import SegmentSerializer
  from central.serializers.fao.node import FilletSerializer
  from central.serializers.fao.node import ThreadSerializer
  
  class NodeEnum(Enum):
      SEGMENT=&quot;SEGMENT&quot;
      FILLET = &quot;FILLET&quot;
      THREAD = &quot;THREAD&quot;
  
  class NodeSerializerProvider(ObjectFactory):
      def get(self, tool_type, **kwargs):
          return self.create(tool_type, **kwargs)
      
  services = NodeSerializerProvider()
  services.register_builder(NodeEnum.SEGMENT.value, SegmentSerializer)
  services.register_builder(NodeEnum.FILLET.value, FilletSerializer)
  services.register_builder(NodeEnum.THREAD.value, ThreadSerializer)
  </code></pre>
  <p><code>profile.py</code></p>
  <pre><code>from rest_framework import serializers
  
  from central.services import node_serializer_provider
  
  
  class ProfileSerializer(serializers.Serializer):
      # TODO find a way to extract the field type and call the provider
      nodes = node_serializer_provider.services.get(&quot;TYPE&quot;, **{many=True})
  </code></pre>
  <h2>EDIT</h2>
  <p>I tried the second solution of @Ken4scholars to use the SerializerMethodField:</p>
  <p>Inside my view I get a request data and I want to check if data send is well format.</p>
  <pre><code>@action(detail=False, methods=['post'])
  def retrieve(self, request):
      
      serializer = ReportSerializer(data=request.data)
      is_ok = serializer.is_valid()
  </code></pre>
  <p>Here the ReportSerializer</p>
  <pre><code>from rest_framework import serializers
  
  from .profile import ProfileSerializer
  from .workpiece import WorkpieceSerializer
  
  class ReportSerializer(serializers.Serializer):
      profile = ProfileSerializer()
      unit = serializers.CharField(max_length=2)
      workpiece = WorkpieceSerializer()
      ...
  </code></pre>
  <p>.</p>
  <pre><code>class ProfileSerializer(serializers.Serializer):
      nodes = serializers.SerializerMethodField()
  
      def get_nodes(self, instance):
          data = [] 
          for n in instance[&quot;nodes&quot;]:
              serializer = services.get(n[&quot;type&quot;], **{&quot;data&quot;: n})
              serializer.is_valid()
              if serializer.errors:
                  pass
                  # Raise an error
              data.append(serializer.data)
          return data
  </code></pre>
  <p>But I got the KeyError nodes doesn't exist... and when I print the <code>instance</code> I got an empty <code>OrderedDict</code></p>
  <p>Even with a simple case like this:</p>
  <pre><code>{ #get from the api
    &quot;profile&quot;: {
      &quot;ok&quot;: {&quot;some_field&quot;: &quot;some_value&quot;}
    }
  }
  </code></pre>
  <p>.</p>
  <pre><code>ok = serializers.SerializerMethodField()
  def get_ok(self, obj):
      print(obj) # get an empty OrderedDict
      return &quot;get_ok&quot;
  </code></pre>