ragtime.sp
ragtime.sp

Reputation: 121

Django rest update one field

I just got started with Django and got stuck on something that I believe should be simple, but I don't know how to do.

I have a model like this one:

id = models.AutoField(primary_key=true)
...
amount = models.IntegerField()
...

Basically, the user will give an amount and the model needs to be updated with the current amount + the amount that the user inputs.

I use serializers to create new objects, but I don't really know how to use them to do this.

Upvotes: 4

Views: 14312

Answers (2)

Abednego
Abednego

Reputation: 657

just use the partial=True as one of your serializer parameter and create an object for your filed that you want to update i.e i want to update the queue status

    data = {'queue_status': 1}
serializer_patient_queue = PatientQueueSaveSerializer(queue_item, data=data, partial=True)

Upvotes: 4

Paolo Stefan
Paolo Stefan

Reputation: 10263

Let's assume the following:

  1. your model is called MyModel
  2. your serializer class is named MyModelSerializer
  3. AmountPartialUpdateView is extending APIView
  4. 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.

Upvotes: 14

Related Questions