DRF中的request.data与Django中的request.body [英] request.data in DRF vs request.body in Django

查看:577
本文介绍了DRF中的request.data与Django中的request.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django REST框架引入了一个Request对象,该对象扩展了常规的HttpRequest,此新对象类型具有 request.data 来访问JSON数据以用于 POST, PUT和 PATCH请求。

Django REST framework introduces a Request object that extends the regular HttpRequest, this new object type has request.data to access JSON data for 'POST', 'PUT' and 'PATCH' requests.

不过,我可以通过访问原始Django HttpRequest类型对象的一部分request.body参数来获取相同的数据。

However, I can get the same data by accessing request.body parameter which was part of original Django HttpRequest type object.

我看到的一个区别是request.data只能访问一次。此限制不适用于request.body。

One difference which I see is that request.data can be accessed only one time. This restriction doesnt apply to request.body.

我的问题是两者之间有什么区别。当应该有一种-最好只有一种-显而易见的方法时,DRF提供了另一种做同一件事的方式的原因是什么。

My question is what are the differences between the two. What is preferred and what is the reason DRF provides an alternative way of doing same thing when There should be one-- and preferably only one --obvious way to do it.

更新:限制了body始终为JSON类型的用例。禁止XML /图像或常规表格数据。每种优点/缺点是什么?

UPDATE: Limiting the usecase where body is always of type JSON. Never XML/ image or conventional form data. What are pros/cons of each?

推荐答案

您应该使用 request.data 。它更加灵活,涵盖了更多的用例,并且可以根据需要进行多次访问。引用文档:

You should use request.data. It's more flexible, covers more use cases and it can be accessed as many times as needed. Quoting the docs:

关于 request.data


< REST框架引入了一个Request对象,该对象扩展了常规的
HttpRequest,并提供了更灵活的请求解析。 Request对象的
核心功能是request.data属性
,它与request.POST类似,但是对使用Web
API更为有用。

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.

request.POST#仅处理表单数据。仅适用于 POST方法。

request.POST # Only handles form data. Only works for 'POST' method.

request.data#处理任意数据。适用于'POST','PUT'和
'PATCH'方法。

request.data # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' methods.

关于 request.body


原始HTTP请求主体为字节字符串。这对于
以不同于常规HTML形式的方式处理数据很有用:二进制
图像,XML有效负载等。要处理常规形式的数据,请使用
HttpRequest.POST。

The raw HTTP request body as a byte string. This is useful for processing data in different ways than conventional HTML forms: binary images, XML payload etc. For processing conventional form data, use HttpRequest.POST.

因此,除非您要处理二进制图像或XML有效负载,否则请勿使用 request.body ,它是ll只是一个简单的字符串,包含请求的主体。始终使用 request.data 它将是完全解析的主体(即Python dict ),这更加方便处理。

So unless you want to handle binary images or XML payload, never use request.body, it'll only be a simple string containing, well, the body of the request. Always use request.data which'll be the fully parsed body (i.e. a Python dict) which is much more convenient to handle.

这篇关于DRF中的request.data与Django中的request.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆