kate
kate

Reputation: 21

DRF How to retrieve auth user info?

Now I can just get info by id. -> user/{id} I want to retrieve self auth user and get his information by default like user

My view

class UserInfoViewSet(mixins.RetrieveModelMixin,
                           viewsets.GenericViewSet):

    permission_classes = (IsAuthenticated,)
    serializer_class = serializers.UserFollowersSerializer

    def get_queryset(self):
        return User.objects.filter(privacy__is_public=True)

Upvotes: 0

Views: 564

Answers (1)

RaideR
RaideR

Reputation: 927

So if you want to get information about the currently logged in user you could do something like this this:

@action(methods=['get'], detail=False)
def current_user(self, request, *args, **kwargs):
    serializer = self.get_serializer(request.user)
    return Response(serializer.data)

Without an additional action, I could think of the following possibilities.

(1) You could overwrite your get_queryset method to just filter for user=self.request.user.

(2) You could overwrite get_object to return self.request.user.

Upvotes: 1

Related Questions