Let's assume the following:
- your model is called
MyModel
- your serializer class is named
MyModelSerializer
AmountPartialUpdateView
is extending APIView
your partial update url is defined like this -that is, model id is
passed in the pk
URL variable and the amount to add is passed in the amount
URL variable:
urlpatterns = patterns('',
# ...
url(r'^model/update-partial/(?P<pk>\d+)/(?P<amount>\d+)$', AmountPartialUpdateView.as_view(), name='amount_partial_update'),
# ...
)
Then, you should implement the correct update logic in the AmountPartialUpdateView.patch()
method. One way to accomplish this is:
from django.shortcuts import get_object_or_404
from rest_framework import Response
class AmountPartialUpdateView(APIView):
def patch(self, request, pk, amount):
# if no model exists by this PK, raise a 404 error
model = get_object_or_404(MyModel, pk=pk)
# this is the only field we want to update
data = {"amount": model.amount + int(amount)}
serializer = MyModelSerializer(model, data=data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
# return a meaningful error response
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
This way, visiting the URL
model/update-partial/123/5
you will increase the amount
of model id 123 by 5 units.