它实际上在做什么(data = request.data)?

'''

def post(self,request):
        serializer=NameSerializers(data=request.data)
        if(serializer.is_valid):
            name=serializer.data.get('name')
            msg='hello {} Wish you happy New Year'.format(name)
            return Response({'msg':msg})
        return Response(serializer.errors,status=400)

'''

我的第一个问题是

  1. data = request.data是获取客户端应用程序已通过JSON发送的数据。
  2. serializer.is_valid是序列化程序类的内置函数,用于检查我们收到的数据是否为有效数据?
  3. serializer.data.get('name')和这行在做什么?
评论
  • 自述
    自述 回复
    1. request.data is basically data sent in the request body from the client.
    2. serializer.is_valid(), yes, it is a function to check data received from the request, you can pass serializer.is_valid(raise_exception=True) to raise an exception in case of invalid data received.
    3. serializer.data is a python dictionary which is being saved in the model, so .get('var_name') is actually accessing the value from the dictionary.

    希望这会有所帮助:)快乐编码