How to PATCH a single field using Django Rest Framework?

后端 未结 5 1861
逝去的感伤
逝去的感伤 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:29

    It does seem to be supported out of the box. On your browser API, navigate to a model detail page, at the bottom next to the HTML Form tab click Raw data, delete everything from the JSON string except the ID field and the field you wish to change, and click PATCH. A partial PATCH update is performed.

    I'm using djangorestframework==3.2.4, and haven't had to do anything to my ViewSets and Serializers to enable this.

    In this exampe we are updating the bool status_field field of the model, and I'm using jquery 2.2.1. Add the following to the :

    
    
    

    Then in a

    :

    
    

    I chose to permit the checkbox to change, then revert it in the case of failure. But you could replace click with mousedown to only update the checkbox value once the AJAX call has succeeded. I think this will lead to people repeatedly clicking the checkbox for slow connections though.

提交回复
热议问题