Django rest framework - self.context doesn't have request attribute
<pre><code>class MyModelSerializer(serializers.ModelSerializer):
    field1 = serializers.CharField()
    field2 = serializers.SerializerMethodField('get_awesome_user')

    def get_current_user(self):
        request = self.context.get("request")
        if request and hasattr(request, "user"):
            return request.user
        return None

   def get_awesome_user(self, obj):
       user = self.get_current_user()
       ## use this user object, do some stuff and return the value
       return ...
</code></pre>

<p>My api(which uses <code>authentication_classes</code> and <code>permission_classes</code>) is using this serializer and the <code>get_current_user</code> function always returns <code>None</code>. when I debug it, I found that self.context is empty dictionary, i.e <code>{}</code>. to be double sure I also printed <code>self.context.keys()</code>, still it's empty list.</p>

<p>I followed this thread.</p>

<p><a href="https://stackoverflow.com/questions/27934822/get-current-user-in-model-serializer">Get current user in Model Serializer</a></p>

<p>PS: I'm using <code>djangorestframework==3.3.3</code>, <code>Django==1.9.1</code></p>

<p>EDIT: adding viewset code</p>

<pre><code>class MyModelViewSet(viewsets.ModelViewSet):

    authentication_classes = (SessionAuthentication, BasicAuthentication, TokenAuthentication)
    permission_classes = (IsAuthenticated,)

    def list(self, *args, **kwargs):
         queryset = MyModel.objects.all()
         page = self.paginate_queryset(queryset)
         if page is not None:
             serializer = MyModelSerializer(page, many=True)
             return self.get_paginated_response(serializer.data)

         serializer = MyModelSerializer(queryset, many=True)
         return Response(serializer.data)
</code></pre>