我已经看到了很多次,到处都是它,但无法弄清楚它的实际含义,这是必不可少的吗?我以前没有在代码中使用此 data._mutable = true或false
,我不确定是否应该使用它。
代码片段看起来有点像这样。
def update(self, request, *args, **kwargs):
instance = self.get_object()
data = request.data
if data.get("something") == "null":
data._mutable = True
data["something"] = None
data._mutable = False
为什么我们需要为数据对象的私有属性分配true或false。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您用作解析器a 代码>   [drf-doc] 或
partparser
  [drf-doc] ,或另一个解析的解析器a >   [django-doc] ,那么queryDict
默认情况下是不可分割的。 That means that it will reject any changes to it, so you can not add, remove or edit key-value pairs.By setting the
._mutable
attribute, you can prevent this from raising errors, and thus mutate theQueryDict
.但这不是很好的做法,因为没有记录您可以通过将querydict
通过设置._ mutable
属性为true
来使其变为可变。通常,您可以使用。If you use as parser a
FormParser
[drf-doc] orMultiPartParser
[drf-doc], or another parser that parses to aQueryDict
[Django-doc], then theQueryDict
is by default immutable. That means that it will reject any changes to it, so you can not add, remove or edit key-value pairs.By setting the
._mutable
attribute, you can prevent this from raising errors, and thus mutate theQueryDict
. But it is not good practice, since it is not documented that you can make theQueryDict
mutable by setting the._mutable
attribute toTrue
. Usually you work with.copy()
[Django-doc] which will return a mutable deep copy, so: