如何使用 Django Rest Framework 修补单个字段? [英] How to PATCH a single field using Django Rest Framework?
问题描述
我有一个包含许多字段的模型MyModel",我想使用 PATCH 方法更新字段状态".我正在使用基于类的视图.有没有办法实现PATCH?
I have a model 'MyModel' with many fields and I would like to update a field 'status' using PATCH method. I'm using class based views. Is there any way to implement PATCH?
推荐答案
Serializers allow通过在初始化序列化器时指定 partial=True
来部分更新.这就是 PATCH
请求的处理方式 默认在通用视图中.
Serializers allow partial updates by specifying partial=True
when initializing the serialzer. This is how PATCH
requests are handled by default in the generic views.
serializer = CommentSerializer(comment, data=request.data, partial=True)
这将允许您更新序列化程序中的单个字段,或根据需要更新所有字段,而不受标准 PUT
请求的任何限制.
This will allow you to update individual fields in a serializer, or all of the fields if you want, without any of the restrictions of a standard PUT
request.
这篇关于如何使用 Django Rest Framework 修补单个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!