如何在请求中传递/引用字典



我已经使用下面的函数进行了API调用,我正在Postman上进行测试。它接受一个id(字典)并删除具有该id的医生。所以解决方案是这样的。问题是,id是硬编码的- id = {"id": 11} .我如何引用它,以便我可以在邮差而不是硬编码中提供它?我试过使用id = request.GET.get("id")并将其添加为参数,但它不适合我。如有任何协助,将不胜感激

Views.py

class DoctorBillingPayments(GenericAPIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
@classmethod
@encryption_check
def delete(self, request, *args, **kwargs):
id = {"id": 11}
try:
result = {}
auth = cc_authenticate()
res = deleteDoctor(auth["key"],id)
result = res
return Response(result, status=status.HTTP_200_OK)
except Exception as e:
error = getattr(e, "message", repr(e))
result["errors"] = error
result["status"] = "error"
return Response(result, status=status.HTTP_400_BAD_REQUEST)

api_service.py

def deleteDoctor(auth, data):
try:

headers = {
"Authorization": f'Token {auth}'
}
url = f'{CC_URL}/doctors/'
print(url)
res = requests.delete(url, json=data, headers=headers)
return res.json()
except ConnectionError as err:
print("connection exception occurred")
print(err)
return err        

我建议使用REST样式,并使用path参数将id传递给视图,如下所示:

  • DELETE /doctor/{id}

为此,您需要在urls中定义参数:

url(r'^doctor/(?P<id>[0-9]+)/$', DoctorBillingPayments.as_view(), name="doctors")

,那么您可以从kwargs访问它,以获得DELETE /doctor/123

等示例请求。
class DoctorBillingPayments(GenericAPIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
@classmethod
@encryption_check
def delete(self, request, *args, **kwargs):
id = int(kwargs['id'])
try:
result = {}
auth = cc_authenticate()
res = deleteDoctor(auth["key"],id)
result = res
return Response(result, status=status.HTTP_200_OK)
except Exception as e:
error = getattr(e, "message", repr(e))
result["errors"] = error
result["status"] = "error"
return Response(result, status=status.HTTP_400_BAD_REQUEST)

最新更新