其余API参数为空Serializer,无模型

时间:2019-03-23 23:51:34

标签: django django-rest-framework

在我看来,我无法检索其余API中的参数。当我打印数据时,我得到{'in_text':''}

serializers.py

class StandardizerSerializer(serializers.Serializer):
    in_text = serializers.CharField(max_length=2000,required=False)

views.py

class standardiz_text(APIView):
    def post(self, request):
        #serializer =  StandardizerSerializer(context={'request': request})
        serializer =  StandardizerSerializer(data=request.data)
        print(repr(serializer))
        if serializer.is_valid(raise_exception=True):
            print(serializer.data)
        else:
            print("----------not valid")
        return Response({'standardizer_text': ' test data'})

邮递员拨打的网址

http://127.0.0.1:8000/standardiz/text/?in_text=tesinput

输出是

StandardizerSerializer(data={}):
    in_text = CharField(max_length=2000, required=False)
{}

1 个答案:

答案 0 :(得分:2)

您正在将 in_text 数据作为 url查询参数 传递。 URL查询参数位于request.query_params属性内。因此,如果您想对query_params中的内容进行序列化,则必须将其传递给序列化器,而不是 request.data

#code
class standardiz_text(APIView):
    def post(self, request):
        serializer = StandardizerSerializer(data=request.query_params) # change is here <<<<<
        if serializer.is_valid(raise_exception=True):
            print(serializer.data)
        else:
            print("----------not valid")
        return Response({'standardizer_text': ' test data'})