Cipher
Cipher

Reputation: 2122

Response From Django Rest Api Framework

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]

Upvotes: 0

Views: 566

Answers (2)

VIVEK VIKASH
VIVEK VIKASH

Reputation: 179

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,
                }            
            )

Upvotes: 4

Sami Kanafani
Sami Kanafani

Reputation: 15741

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

Upvotes: 2

Related Questions