title: How to deserialize POST request using a different Serializer
body: <p>I'm trying to handle POST requests in my API endpoint using a different serializer than the one derived from Model and used for GET or PUT requests. The format of the POSTed messages is different from the Model and from GET/PUT and must be pre-processed before storing to the database. </p>
  
  <p>As a demo of my problem I made a very simple model and the corresponding API view and serializer:</p>
  
  <pre><code>class Message(models.Model): 
      message = models.CharField(max_length = 500)
  
  class MessageSerializer(serializers.ModelSerializer):
      class Meta:
          model = Message
          fields = ('message',)
  
  class MessageViewSet(viewsets.ModelViewSet):
      queryset = Message.objects.all().order_by('-pk')
      serializer_class = MessageSerializer
  </code></pre>
  
  <p>That works well. Then I tried to override the MessageViewSet.create() to handle POST requests differently. </p>
  
  <pre><code>class MessageSerializer_FromTo(serializers.Serializer):
      sender = serializers.EmailField()
      recipient = serializers.EmailField()
  
      def create(self, validated_data):
          message = "Message from &lt;{sender}&gt; to &lt;{recipient}&gt;".format(**validated_data)
          return Message(message)
  
  class MessageViewSet(viewsets.ModelViewSet):
      queryset = Message.objects.all().order_by('-pk')
      serializer_class = MessageSerializer
  
      # Handle POST requests differently
      def create(self, request, format=None):
          message = MessageSerializer_FromTo(data = request.data)
          if message.is_valid():
              message.save()
              return Response(message.data, status=status.HTTP_201_CREATED)
          return Response(message.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>Essentially I want to pass this JSON to POST /api/messages/ </p>
  
  <pre><code>{"sender": "some@one.com", "recipient": "someone@else.org"}
  </code></pre>
  
  <p>GET /api/messages/1/ should return</p>
  
  <pre><code>{"message": "Message from &lt;some@one.com&gt; to &lt;someone@else.org&gt;"}
  </code></pre>
  
  <p>However the POST fails with this message:</p>
  
  <pre><code>Internal Server Error: /api/messages/
  Traceback (most recent call last):
    File ".../rest_framework/fields.py", line 441, in get_attribute
      return get_attribute(instance, self.source_attrs)
    File ".../rest_framework/fields.py", line 100, in get_attribute
      instance = getattr(instance, attr)
  AttributeError: 'Message' object has no attribute 'sender'
  
  
  During handling of the above exception, another exception occurred:
  [...]
  AttributeError: Got AttributeError when attempting to get a value for field `sender` on serializer `MessageSerializer_FromTo`.
  The serializer field might be named incorrectly and not match any attribute or key on the `Message` instance.
  Original exception text was: 'Message' object has no attribute 'sender'.
  [26/Feb/2018 05:09:08] "POST /api/messages/ HTTP/1.1" 500 19059
  </code></pre>
  
  <p>This is just to demonstrate the problem obviously, I'm doing more complex things in my POST handler, but the error is like this.</p>
  
  <p>Any idea how to achieve what I need? I.e. accept POST fields that are completely different from the Model fields?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE:</strong> The complete code is here: <a href="https://github.com/mludvig/drf-demo" rel="nofollow noreferrer">https://github.com/mludvig/drf-demo</a></p>