Home > front end >  How can i send updated request from 1 api to another api
How can i send updated request from 1 api to another api

Time:11-11

I'm calling update_profile api by which return Response(self.profile(request).data) is suppose tp give updated users profile. but the thing is it is not giving output in real time. If i change name it is not reflecting in single api hit. Changes being reflect in 2nd api hit. Because of same request is being passed to profile api. So How can i update request and then send it to self.profile

Heres my code:

@action(detail=False, methods=['get'], authentication_classes=(JWTAuthentication, SessionAuthentication))
def profile(self, request):
    user = self.get_user()
    if not user:
        raise AuthenticationFailed()
    return Response(
        {"status": 200, "message": "Success", "data": UserSerializer(user, context={"request": self.request}).data},
        status=status.HTTP_200_OK)


@action(detail=False, methods=['post'], authentication_classes=(JWTAuthentication, SessionAuthentication))
def update_profile(self, request):

    profile_serialize = ProfileSerializer(data=request.data)
    profile_serialize.is_valid(raise_exception=True)

    AppUser.objects.filter(pk=self.get_user().pk).update(**profile_serialize.data)

    return Response(self.profile(request).data)

CodePudding user response:

You can do something like this to get the updated user.

@action(detail=False, methods=['get'], authentication_classes=(JWTAuthentication, SessionAuthentication))
def profile(self, request):
    user = self.get_user()
    if not user:
        raise AuthenticationFailed()
    else:
        user = AppUser.objects.get(pk=self.get_user().pk)
    return Response(
        {"status": 200, "message": "Success", "data": UserSerializer(user).data},
        status=status.HTTP_200_OK)


@action(detail=False, methods=['post'], authentication_classes=(JWTAuthentication, SessionAuthentication))
def update_profile(self, request):

    profile_serialize = ProfileSerializer(data=request.data)
    profile_serialize.is_valid(raise_exception=True)

    AppUser.objects.filter(pk=self.get_user().pk).update(**profile_serialize.data)

    return Response(self.profile(request).data)

  • Related