title: Rest framework: different serializer for input and output data on post/put operations
body: <p>Lets say I have these models:</p>
  
  <pre><code>class Download(MPTTTimeStampedModel):
      endpoint = models.ForeignKey(EndPoint, related_name="downloads",)
  
  class EndPoint(TimeStampedModel):
      name = models.CharField(max_length=100, verbose_name=_(u"Nombre"))
      url = models.CharField(max_length=2000, verbose_name=_(u"Url"))
  </code></pre>
  
  <p>These serializers:</p>
  
  <pre><code>class DownloadSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Download
          fields = ('id', 'endpoint')  
  
      def create(self, validated_data):
          ...
  
      def update(self, validated_data):
          ...
  
  
  class EndPointSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = EndPoint
          fields = ('id', 'name', 'url')
  
      def create(self, validated_data):
          ...
  
      def update(self, validated_data):
          ...
  </code></pre>
  
  <p>And this generic api view:</p>
  
  <pre><code>class DownloadList(generics.ListCreateAPIView):
      queryset = Download.objects.all()
      serializer_class = DownloadSerializer
  </code></pre>
  
  <p>This will allow me to create a download by sending a json representation looking like this:</p>
  
  <pre><code>{
      'id': null,
      'endpoint': 19
  }
  </code></pre>
  
  <p>And upon creation, the web service will send me back the data with the id from the database. Now, I actually want the web service to send me back not just the endpoint id but a complete representation of the object, Something like this:</p>
  
  <pre><code>{
      'id': null,
      'endpoint': {
          'id': 19,
          'name': 'my endpoint',
          'url': 'http://www.my-endpoint.com/'
      }
  }
  </code></pre>
  
  <p>I would manage this with this serializer:</p>
  
  <pre><code>class DownloadDetailedSerializer(DownloadSerializer): 
      endpoint = EndPointSerializer(many = False, read_only=False)
  </code></pre>
  
  <p>And now the actual question: how do i tell my generic view to use this last serializer for the returned data while keeping the original DownloadSerializer for the input?</p>
  
  <p><strong>EDIT</strong>: as @neverwalkeralone suggested the way to go is creating a custom field and overriding to_representation method. But that lead me to an exception in the line <em>serializer = EndPointSerializer(value)</em>, and after some testing I found out that it would be better to inherit my custom field from RelatedField. That implies overriding to_internal_value too. So here is what finally got the job done:</p>
  
  <pre><code>class EndPointField(serializers.RelatedField):
  
      def to_representation(self, value):
          serializer = EndPointSerializer(value)
          return serializer.data
  
      def to_internal_value(self, instance):
          endpoint = EndPoint.objects.get(pk=instance)
          return endpoint
  
      def get_queryset(self):
          return EndPoint.objects.all()
  </code></pre>