Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to PATCH a single field using Django Rest Framework?

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?

like image 649
pnhegde Avatar asked Jan 16 '15 09:01

pnhegde


People also ask

How do I update a field in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.


1 Answers

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.

like image 73
Kevin Brown-Silva Avatar answered Oct 05 '22 12:10

Kevin Brown-Silva