Manav khanna
Manav khanna

Reputation: 11

what actually it is doing (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)


My questions are:

  1. Is data=request.data getting the data the client app has been sending in JSON?
  2. Is serializer.is_valid an in-built function of serializer class to check the data we have received is valid data or not?
  3. What is serializer.data.get('name') doing?

Upvotes: 0

Views: 100

Answers (1)

sandeshdaundkar
sandeshdaundkar

Reputation: 903

  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.

Hope this helps :) Happy Coding

Upvotes: 1

Related Questions