How to PATCH a single field using Django Rest Framework?

后端 未结 5 1864
逝去的感伤
逝去的感伤 2020-12-23 12:31

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 PAT

5条回答
  •  臣服心动
    2020-12-23 13:26

    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)
    

    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.

提交回复
热议问题