DRF如何在不使用查询集的情况下调用api



我正在尝试在DRF中创建一个没有任何DB的rest API。我希望用户使用发布数据访问API。一旦系统接收到数据,我将执行另一个API调用并显示响应。我的代码是:

serializers.py

class getCPHSerializers(serializers.Serializer):
cph_id = serializers.CharField(max_length=128, write_only=True, required=True)

views.py

class GETCPHDetails(generics.ListAPIView):
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, *args, **kwargs):
cphID = request.data.get('cphID',None)
errorList = []
if not cphID:
errorList.append({"message": "No CPH Found"})
if len(errorList) == 0:
param = {"searchQry": cphID}
apiResponse = requests.post("http://172.61.25.40:8000/api/newSearch", data=param )
return Response({"message":json.loads(apiResponse)})

我收到一个错误

期望从视图返回ResponseHttpResponseHttpStreamingResponse,但收到了<class 'NoneType'>

任何建议都会有很大帮助。提前谢谢。

您看到此错误是因为时没有返回响应

len(errorList) == 0False

如果不返回任何内容,Python将始终返回None来自方法。

最新更新