53
votes

I am not very experience with Django REST framework and have been trying out many things but can not make my PATCH request work.

I have a Model serializer. This is the same one I use to add a new entry and ideally I Would want to re-use when I update an entry.

class TimeSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimeEntry
        fields = ('id', 'project', 'amount', 'description', 'date')

    def __init__(self, user, *args, **kwargs):
        # Don't pass the 'fields' arg up to the superclass
        super(TimeSerializer, self).__init__(*args, **kwargs)
        self.user = user

    def validate_project(self, attrs, source):
        """
        Check that the project is correct
        """
        .....

    def validate_amount(self, attrs, source):
        """
        Check the amount in valid
        """
        .....

I tried to use a class based view :

class UserViewSet(generics.UpdateAPIView):
    """
    API endpoint that allows timeentries to be edited.
    """
    queryset = TimeEntry.objects.all()
    serializer_class = TimeSerializer

My urls are:

url(r'^api/edit/(?P<pk>\d+)/$', UserViewSet.as_view(), name='timeentry_api_edit'),

My JS call is:

var putData = { 'id': '51', 'description': "new desc" }
$.ajax({
    url: '/en/hours/api/edit/' + id + '/',
    type: "PATCH",
    data: putData,
    success: function(data, textStatus, jqXHR) {
        // ....
    }
}

In this case I would have wanted my description to be updated, but I get errors that the fields are required(for 'project'and all the rest). The validation fails. If add to the AJAX call all the fields it still fails when it haves to retrieve the 'project'.

I tried also to make my own view:

@api_view(['PATCH'])
@permission_classes([permissions.IsAuthenticated])
def edit_time(request):

    if request.method == 'PATCH':
        serializer = TimeSerializer(request.user, data=request.DATA, partial=True)
        if serializer.is_valid():
            time_entry = serializer.save()
        return Response(status=status.HTTP_201_CREATED) 
    return Response(status=status.HTTP_400_BAD_REQUEST) 

This did not work for partial update for the same reason(the validation for the fields were failing) and it did not work even if I've sent all the fields. It creates a new entry instead of editing the existing one.

I would like to re-use the same serializer and validations, but I am open to any other suggestions. Also, if someone has a piece of working code (ajax code-> api view-> serializer) would be great.

6
This is kind of an old problem but I'm curious to see what happens if you: (1) Changing the request type to POST and (2) adding a _method field with value PATCH. See here. Django rest framework should take care of partial updates, so it should Just Work. - Jamie
I changed to using PUT instead of PATCH and just send everything, because i had access to get all the data and it worked like that. I remember I also found out something and adapted my code. Maybe now it would work with PATCH. Sorry, i do not have more information. It was a while ago. - Vlad
So can you post your working code? @Vlad - KhoPhi
I made it work with PATCH some time ago, I'm digging through my old code to see what I did. I was using angular-resource and I remember having configured the update endpoint with PATCH. Have you tried using viewsets.ModelViewSet instead of generics.UpdateAPIView? - Lorenzo Peña
@Rexford: If the code is the same (the class based version not functional one) and make a PUT it was working. - Vlad

6 Answers

55
votes
class DetailView(APIView):
    def get_object(self, pk):
        return TestModel.objects.get(pk=pk)

    def patch(self, request, pk):
        testmodel_object = self.get_object(pk)
        serializer = TestModelSerializer(testmodel_object, data=request.data, partial=True) # set partial=True to update a data partially
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(code=201, data=serializer.data)
        return JsonResponse(code=400, data="wrong parameters")

Documentation
You do not need to write the partial_update or overwrite the update method. Just use the patch method.

12
votes

Make sure that you have "PATCH" in http_method_names. Alternatively you can write it like this:

@property
def allowed_methods(self):
    """
    Return the list of allowed HTTP methods, uppercased.
    """
    self.http_method_names.append("patch")
    return [method.upper() for method in self.http_method_names
            if hasattr(self, method)]

As stated in documentation:

By default, serializers must be passed values for all required fields or they will raise validation errors. You can use the partial argument in order to allow partial updates.

Override update method in your view:

def update(self, request, *args, **kwargs):
    instance = self.get_object()
    serializer = TimeSerializer(instance, data=request.data, partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save(customer_id=customer, **serializer.validated_data)
    return Response(serializer.validated_data)

Or just override method partial_update in your view:

def partial_update(self, request, *args, **kwargs):
    kwargs['partial'] = True
    return self.update(request, *args, **kwargs)

Serializer calls update method of ModelSerializer(see sources):

def update(self, instance, validated_data):
    raise_errors_on_nested_writes('update', self, validated_data)

    # Simply set each attribute on the instance, and then save it.
    # Note that unlike `.create()` we don't need to treat many-to-many
    # relationships as being a special case. During updates we already
    # have an instance pk for the relationships to be associated with.
    for attr, value in validated_data.items():
        setattr(instance, attr, value)
    instance.save()

    return instance

Update pushes the validated_data values to the given instance. Note that update should not assume all the fields are available. This helps to deal with partial updates (PATCH requests).

7
votes

The patch method is worked for me using viewset in DRF. I'm changing you code:

class UserViewSet(viewsets.ModelViewSet):
    queryset = TimeEntry.objects.all()
    serializer_class = TimeSerializer

    def perform_update(self, serializer):
        user_instance = serializer.instance
        request = self.request
        serializer.save(**modified_attrs)
        return Response(status=status.HTTP_200_OK)
0
votes

Use ModelViewSet instead and override perform_update method from UpdateModelMixin

class UserViewSet(viewsets.ModelViewSet):
    queryset = TimeEntry.objects.all()
    serializer_class = TimeSerializer

    def perform_update(self, serializer):
        serializer.save()
        # you may also do additional things here
        # e.g.: signal other components about this update

That's it. Don't return anything in this method. UpdateModelMixin has implemented update method to return updated data as response for you and also clears out _prefetched_objects_cache. See the source code here.

0
votes

I ran into this issues as well, I solved it redefining the get_serializer_method and adding custom logic to handle the partial update. Python 3

 class ViewSet(viewsets.ModelViewSet):
     def get_serializer_class(self):
         if self.action == "partial_update":
             return PartialUpdateSerializer

Note: you may have to override the partial_update function on the serializer. Like so:

class PartialUpdateSerializer(serializers.Serializer):
    def partial_update(self, instance, validated_data):
       *custom logic*
       return super().update(instance, validated_data)
0
votes

Another posiblity is to make the request by URL. For example, I have a model like this

      class Author(models.Model):
        FirstName = models.CharField(max_length=70)
        MiddleName = models.CharField(max_length=70)
        LastName = models.CharField(max_length=70)
        Gender = models.CharField(max_length=1, choices = GENDERS)
        user = models.ForeignKey(User, default = 1, on_delete = models.CASCADE, related_name='author_user')
        IsActive = models.BooleanField(default=True)
        class Meta:
          ordering = ['LastName']

And a view like this

      class Author(viewsets.ModelViewSet):
        queryset = Author.objects.all()
        serializer_class = AuthorSerializer

So can enter http://127.0.0.1:8000/author/ to get or post authors. If I want to make a PATCH request you can point to http://127.0.0.1:8000/author/ID_AUTHOR from your client. For example in angular2, you can have something like this

       patchRequest(item: any): Observable<Author> {
        return this.http.patch('http://127.0.0.1:8000/author/1', item);
       }

It suppose you have configured your CORS and you have the same model in back and front. Hope it can be usefull.