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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
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 clickRaw data
, delete everything from the JSON string except the ID field and the field you wish to change, and clickPATCH
. A partialPATCH
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<head>
:Then in a
<form>
:I chose to permit the checkbox to change, then revert it in the case of failure. But you could replace
click
withmousedown
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.Serializers allow partial updates by specifying
partial=True
when initializing the serialzer. This is howPATCH
requests are handled by default in the generic views.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.As Kevin Brown stated you could use the
partial=True
, which chefarov clarified nicely.I would just like to correct them and say you could use generics freely, depending on the HTTP method you are using:
If you are using PATCH HTTP method like asked, you get it out of the box. You can see
UpdateModelMixin
code forpartial_update
:For any HTTP method different from PATCH, this can be accomplished by just overriding the
get_serializer
method as follows:This will create the serializer as partial, and the rest of the generics will work like a charm without any manual intervention in the update/partial_update mechanism.
Under the hood
I used the generic:
generics.UpdateAPIView
which uses theUpdateModelMixin
which has this code:So, if you override the
get_serializer
function, you can actually override the partial argument and force it to be true.Please note, that if you want it partial only for some of the HTTP methods, this makes this approach more difficult.
I am using djangorestframework==3.5.3
I struggled with this one for a while, but it is a very straightforward implementation using generic views or a combination of generic views and mixins.
In the case of using a generic update view (generics.UpdateAPIView), just use the following code, making sure the request type is PATCH:
There's nothing else to it!
If you're using an update mixin (mixins.UpdateModelMixin) in combination with a generic view (generics.GenericAPIView), use the following code, making sure the request type is PATCH:
The second example is more complex, showing you how to override the queryset and lookup field, but the code you should pay attention to is the patch function.
If anyone is still planning to find a simple solution using
ModelSerializer
without changing much of your views, you can subclass theModelSerializer
and have all yourModelSerializer
s inherit from that.