0

I have the following 2 views and on a specific param I need to send a PATCH request to another view on receiving certain params. However i get the following error, how to rectify this?

Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, 
but received a `<class 'requests.models.Response'>`

The view are as follows:

class Emp_Status_Trans_ListView(APIView):
    permission_classes = [DjangoCustomModelPermissions]
    queryset = Emp_Status_Trans.objects.none()

    def get(self, request, format=None):
        emp_mast_id=request.query_params.get('employee',None)
        linked_model_data = [("hr","Emp_Status_Mast","Emp_Status_Mast_Serializer",("emp_status_mast_id","emp_status_short"))]
        final_resp = {}
        db_data = Emp_Status_Trans.objects.all().prefetch_related("emp_mast","emp_status_mast")
        if emp_mast_id:
            db_data = db_data.filter(emp_mast=emp_mast_id)
        serializer = Emp_Status_Trans_Serializer(db_data, many=True)
        final_resp['emp_status_trans'] = serializer.data
        get_linked_data(final_resp, linked_model_data)
        return Response(final_resp)

    def post(self, request, format=None):
        patch_emp_mast=request.query_params.get('patch_emp_mast',None)
        serializer = Emp_Status_Trans_Serializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            if patch_emp_mast:
                return self.patch_emp_mast(serializer.data,request.META.get("HTTP_HOST"),request.META.get("HTTP_AUTHORIZATION"))
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def patch_emp_mast(self,data,domain,access_token):
        url = "http://"+domain+"/hr/emp_mast/"+str(data['emp_mast']['id'])+"/"
        headers = {'Content-Type': 'application/json', 'Authorization':access_token}
        data = {
            'emp_status_mast': data['emp_status_mast'],
        }
        return requests.patch(url,headers=headers, data=json.dumps(data))

patch_emp_mast is called when query param is received however it fails with the error mentioned earlier. How to rectify this?

2
  • You are returning not Django's response, but something else Commented Nov 5, 2020 at 8:36
  • How to I convert the return from library requests i.e. <class 'requests.models.Response'> to Django Response Commented Nov 5, 2020 at 8:37

1 Answer 1

2

you can rewrite your code like ;

 def patch_emp_mast(self,data,domain,access_token):
    url = "http://"+domain+"/hr/emp_mast/"+str(data['emp_mast']['id'])+"/"
    headers = {'Content-Type': 'application/json', 'Authorization':access_token}
    data = {
        'emp_status_mast': data['emp_status_mast'],
    }
    try:

      response= requests.patch(url,headers=headers, data=json.dumps(data))
      return Response("status":True,"response":response.json())

    expect:
         return Response("status":False,"response":{})

Best regards

Sign up to request clarification or add additional context in comments.

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.