在我看来,我无法检索其余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)
{}
答案 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'})