0

I have created lots of API's list this using Django Rest Framework and it is working perfectly fine. But the issue i am facing is that when these API's are executed succesfully, then i am not getting Response like status=true or any response which says it executed successfully. Is there in-built function in rest framework or how can i do it to get success message.

# Add Trusty
class TrustyAddAPIView(generics.CreateAPIView):
    queryset = TrustyRequest.objects.all()
    serializer_class = serializers.TrustyAddSerialzer
    permission_classes = [IsAuthenticated]


# User Trusty Profile Update
class TrustyUserProfileUpdateAPIView(generics.RetrieveUpdateAPIView):
    queryset = User.objects.all()
    serializer_class = serializers.UserDetailSerialzer
    permission_classes = [IsAuthenticated]
1
  • 3
    You will get 2XX status code if the request succeeded. You could use the same to identify the status Commented Mar 1, 2019 at 7:18

2 Answers 2

4

You can override the get and post method and in return you can send response as:

class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = serializers.UserListSerialzer
    permission_classes = [IsAuthenticated]

    def list(self, request):
        # Note the use of `get_queryset()` instead of `self.queryset` 
        queryset = self.get_queryset()
        serializer = self.serializer_class(queryset, many=True)
        return Response(
                {
                    "result": serializer.data,
                    "message":"Testimonials Retrieved Successfully.",
                    "status" : True,
                }            
            )
Sign up to request clarification or add additional context in comments.

Comments

2

when the API is executed successfully, you should return a 200 response. This done in django like this:

return Response(serializer.data, status=status.HTTP_200_OK)

read more about HTTP status code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.