【问题标题】:DRF how to call an api without the the querusetDRF如何在没有queruset的情况下调用api
【发布时间】:2020-10-20 08:47:19
【问题描述】:

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

序列化器.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'>

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

【问题讨论】:

标签: django-rest-framework


【解决方案1】:

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

len(errorList) == 0False

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多