在 return 'Response' (python) 中传递多个参数

Passing multiple arguments in return 'Response' (python)

我在 Angular 工作,我正在使用 Http 请求和响应。 是否可以在 'Response'.

中发送多个参数

Angular 文件:

this.http.get("api/agent/applicationaware").subscribe((data:any)...

python 文件:

def get(request):
    ...
    return Response(serializer.data)

我想在响应中发送多个参数。 喜欢

return Response(serializer.data,obj.someothervalue)

你能帮我解决这个问题吗? 提前致谢:)

你可以return一个dictionary

return Response({'serializer_data': serializer.data, 'some_other_value': obj.someothervalue})

或者您可以将 someothervalue 附加到 serializer.data

data = serializer.data
data['someothervalue'] = obj.someothervalue
return Response(data)

如果obj.someothervalue也是一个字典,那么你可以合并两个字典:

data = serializer.data.copy()
data.update(obj.someothervalue)
return Response(data)

添加字典

dict = {}
dict['details'] = serializer.data
dict['other'] = someotherdata
return Response(dict)

希望这对您有所帮助

您需要先确定 API 响应的协议,然后再进行相应的计划。

为了回答您的问题,是的,您可以发送多个参数作为响应。

您可以创建一个 dictionary/list 并将要发送的所有值放入其中。现在发送 dictionary/list 作为您的回复。 我会推荐这样的东西:

response = dict()
response["data"] = dict()
response["data"]["serializer_data"] = serializer.data
response["data"]["some_other_value"] = obj.someothervalue

return Response(response)

在响应的接收端,您必须以与发送它类似的方式读取响应数据。