django - Key error 'request' in serializer -
i'm trying current user in serializer, evaluate condition (did user post?), this answer tried doesn't seem work:
def current_user(self): return self.context['request'].user class postserializer(serializers.modelserializer): liked = serializers.serializermethodfield() def get_liked(self, obj): return str(current_user(self)) class meta: model = track fields = ('foo', 'liked')
and simple view:
@api_view(['get']) def post_item(request, pk): serializer = postserializer(post) return response(serializer.data)
when trying get_liked
error raised:
keyerror @ /api/posts/1
'request'
am missing here?
you can send context view serializer this:
@api_view(['get']) def post_item(request, pk): serializer = postserializer(track, context={'request': request}) return response(serializer.data)
Comments
Post a Comment