How to PATCH a single field using Django Rest Framework?

pnhegde picture pnhegde · Jan 16, 2015 · Viewed 33.1k times · Source

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?

Answer

Kevin Brown picture Kevin Brown · Jan 16, 2015

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.