Search code examples
pythondjangodjango-rest-frameworkpostman

Success message not showing after deleting the object in Django rest framework


I have a DestroyAPIView in which I am using the perform_delete function to delete an instance. However, I want to send a success message with 200 status. I tried, but in the postman I am getting blank with 204 status. How can I achieve this?

My view:

class UpdateOrderView(UpdateAPIView,DestroyAPIView):
    permission_classes = [AllowAny]
    #queryset = Order.objects.prefetch_related('order_items').all()
    #value = self.kwargs['pk']
    queryset = Order.objects.all()
    print(queryset)
    serializer_class = OrderUpdateSerializer

    def perform_destroy(self, instance):
        if instance.delete():
            return Response({
                "message":"Order deleted successfully"
            },
            status=status.HTTP_200_OK)
        else:
            pass

The instance is deleted, I have checked the database, but I am not getting success message.


Solution

  • You can override destroy method instead.

    class UpdateOrderView(UpdateAPIView,DestroyAPIView):
        permission_classes = [AllowAny]
        queryset = Order.objects.all()
        serializer_class = OrderUpdateSerializer
    
        def destroy(self, request, *args, **kwargs):
            instance = self.get_object()
            self.perform_destroy(instance)
            return Response({
                "message":"Order deleted successfully"
            },
            status=status.HTTP_200_OK)
    
        def perform_destroy(self, instance):
            instance.delete()