request.data 似乎是一个可能不好吃的生鸡蛋,并且包含有关请求的额外信息。根据您的序列化程序设置,Serializer.data 类似于煮熟的鸡蛋菜。
并且 request.data 将用作序列化程序的输入。如果在检查序列化器的验证后请求数据有效,则可以保存序列化器以创建对象实例。如果不是,则序列化程序无效。如果所有这些过程都成功,您就可以访问 serializer.data ,它是一个格式化的字典。
这是 DRF 中request data 的定义。
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.
这里是 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"
]
}
以及序列化器的数据示例:
{
"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"
}