django rest framework SerializerMethodField bizarre behaviour
<p>When two serializer classes declare a SerializerMethodField with same name, like this:</p>

<pre><code>class APIProfileSerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):

        super(APIProfileSerializer, self).__init__(*args, **kwargs)
        self.request = request

    def get_image(self, instance):

        if instance.image:
            return self.request.build_absolute_uri(location=instance.image.url)
        return None

    image = serializers.SerializerMethodField()

    # other ProfileSerializer fields

    class Meta:
        model = User
        fields = ('image')



class ProfileSerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):

        super(ProfileSerializer, self).__init__(*args, **kwargs)
        self.request = request

    def get_image(self, instance):

        if instance.image:
            return self.request.build_absolute_uri(location=instance.image.url)
        return None

    image = serializers.SerializerMethodField()

    # other ProfileSerializer fields

    class Meta:
        model = User
        fields = ('image')
</code></pre>

<p>the code </p>

<pre><code>serializer = APIProfileSerializer(instance=request.user, request=request)
return Response(data=serializer.data)
</code></pre>

<p>crashes with the following traceback:</p>

<blockquote>
  <p>Traceback (most recent call last):
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/django/core/handlers/base.py", line 174, in get_response
      response = self.process_exception_by_middleware(e, request)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in get_response
      response = response.render()
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/django/template/response.py", line 160, in render
      self.content = self.rendered_content
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/response.py", line 71, in rendered_content
      ret = renderer.render(self.data, media_type, context)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/renderers.py", line 675, in render
      context = self.get_context(data, accepted_media_type, renderer_context)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/renderers.py", line 618, in get_context
      raw_data_put_form = self.get_raw_data_form(data, view, 'PUT', request)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/renderers.py", line 540, in get_raw_data_form
      content = renderer.render(serializer.data, accepted, context)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/serializers.py", line 503, in data
      ret = super(Serializer, self).data
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/serializers.py", line 239, in data
      self._data = self.to_representation(self.instance)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/serializers.py", line 472, in to_representation
      ret[field.field_name] = field.to_representation(attribute)
    File "/home/ubuntu/venv/superhq_rest/lib/python3.4/site-packages/rest_framework/fields.py", line 1645, in to_representation
      return method(value)
    File "/home/ubuntu/vhosts/superhq_backend/account/serializers.py", line 32, in get_image
      return self.request.build_absolute_uri(location=instance.image.url)
  AttributeError: 'NoneType' object has no attribute 'build_absolute_uri'</p>
</blockquote>

<p>( my first question is: how to make traceback more readable in markdown? =) )</p>

<p>It's like <code>request</code> hasn't been passed to serializer</p>

<p>however, this</p>

<pre><code>serializer = ProfileSerializer(instance=request.user, request=request)
return Response(data=serializer.data)
</code></pre>

<p>and this</p>

<pre><code>serializer = APIProfileSerializer(instance=request.user,request=request)
_data = serializer.data,
return Response(data=_data)
</code></pre>

<p>work fine...</p>

<p>Debugging show that BOTH APIProfileSerializer and ProfileSerializer are called, and the crash is caused by calling <code>get_image</code> in <code>ProfileSerializer</code> without passing <code>request</code> to it</p>

<p>So, the question is: WTF?! I didn't call this serializer! I never even mentioned it in my views! How and for what reason is it being called?</p>

<p>I stick to the latter code piece to solve this problem, but there's got to be a better way! Any suggestions?</p>