简体   繁体   中英

request.data in DRF v/s serializers.data in DRF

What is the difference between request.data in DRF and serializers.data in DRF.

When I write function based view in DRF, I use both of them like -

        elif request.method == 'POST':  

        serializer = datesSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()   

and,

        startdate = serializer.data['startdate']
        enddate = serializer.data['enddate']

But couldn't find the difference about them and difference on using them in the code.

The request.data seems a raw egg that might not be tasty and contains extra information about the request. Serializer.data is similar to a cooked egg dish based on your serializer's settings.

And request.data will be used as an input for the serializer. If the request data is valid after checking serializer's validation, then the serializer can be saved to create an object instance. If not, the serializer is not valid. If all those procedures are successful, you can then access serializer.data which is a formatted dictionary.

Here is the definition of request data in DRF.

request.data returns the parsed content of the request body. This is similar to the standard request.POST and request.FILES attributes except that:

It includes all parsed content, including file and non-file inputs.
It supports parsing the content of HTTP methods other than POST, meaning that you can access the content of PUT and PATCH requests.
It supports REST framework's flexible request parsing, rather than just supporting form data. For example you can handle incoming JSON data in the same way that you handle incoming form data.
For more details see the parsers documentation.

Here is an example of request.data

{
   "csrfmiddlewaretoken":[
      "RrwwZyZCmEElmGG16muxEopwXbRZDsARYcDjraIC1kmcjEux3OIOZoeG7XUSmL4V"
   ],
   "email":[
      "myqepuzez@mailinator.com"
   ],
   "first_name":[
      "Roary"
   ],
   "last_name":[
      "Daugherty"
   ],
   "password":[
      "In quam qui magni re"
   ],
   "phone":[
      "+1 (542) 262-5207"
   ],
   "username":[
      "tikit"
   ]
}

And an example of the serializer's data:

{
   "id":3,
   "group_ids":[
   ],
   "role_ids":[
   ],
   "email":"myqepuzez@mailinator.com",
   "first_name":"Roary",
   "is_active":False,
   "last_name":"Daugherty",
   "phone":"+1 (542) 262-5207",
   "username":"tikit"
}

request.data is the data which have came as part of request object.

REST framework introduces a Request object that extends the regular HttpRequest, and provides more flexible request parsing. The core functionality of the Request object is the request.data attribute, which is similar to request.POST, but more useful for working with Web APIs.

https://www.django-rest-framework.org/tutorial/2-requests-and-responses/#request-objects

Whereas

serializer.data is the data which have already been parsed by serializer.

Returns the outgoing primitive representation

https://www.django-rest-framework.org/api-guide/serializers/#baseserializer

Reference : request.data in DRF vs request.body in Django

While working with Rest API's, we receive the data from client in "request" and when we print request.data , it will print all the information that is submitted by the client.

The serializers.data is basically used for the data that is to be sent to client or the end user. Sometimes our database has some values which are arranged in such a way that they are not json serializable (let's say that we can not send them to the client), so serializer is something that converts the data into a format that can be transferred to the client.

Parsed by serializer is nothing but data arranged in such a form that it is prepared for sending to the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM