title: PATCH request to ModelViewSet in django rest framework - how do I implement serializer.update()?
body: <p>I'm trying to implement <code>PATCH</code> handling using a <code>ModelViewSet</code> for my Product API. (The Product instance primary key is a <code>uuid</code>, and hence the <code>uuid</code> appears as the <code>lookup_field</code> in the code below). </p>
  
  <p>I'm confused about how to override the <code>update()</code> ModelSerializer class method. The stub in the source looks like this:</p>
  
  <pre><code>def update(self, instance, validated_data):
      raise NotImplementedError('`update()` must be implemented.')
  </code></pre>
  
  <p>I don't understand why <code>instance</code> and <code>validated_data</code> should be provided, because they should be properties of the serializer as per its instantiation:
  <code>get_serializer(self, instance=None, data=None, many=False, partial=False)</code> (scroll up one paragraph <a href="http://www.django-rest-framework.org/api-guide/generic-views/#mixins" rel="nofollow noreferrer">here in the documentation</a>). And why should I be providing the validated data? Isn't the serializer is supposed to validate it?</p>
  
  <p>Here's my <code>partial_update()</code> implementation:</p>
  
  <p>product.py (ViewSet class)</p>
  
  <pre><code>def get_object(self, uuid=None):
      """ Utility method to get the object """
      # authentication logic, returns None if object not found
  
  def partial_update(self, request, uuid=None):
      instance = self.get_object(uuid)
      if not instance:
          return Response(status=status.HTTP_404_NOT_FOUND)
      serializer = self.get_serializer(instance, 
                      data=request.data,
                      many=isinstance(request.data, list),
                      partial=True)
      if not serializer.is_valid():
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
      serializer.update() #NOT WORKING; NEEDS ARGS instance, validated_data
      return Response(serializer.data, status=status.HTTP_200_OK)
  </code></pre>
  
  <p>serializers.py</p>
  
  <pre><code>def update(self, instance, validated_data):
      # we link up the user foreign key here
      user=User.objects.filter(email=validated_data.pop('user', None)).first()
      if user:
          instance.fk_user = user
  
      #specify which fields to update, otherwise save() tries to SQL SET all fields
      update_fields = [k for k in validated_data]
      if user:
          update_fields.append('fk_user')
      for k,v in validated_data.items():
          setattr(instance, k, v)
      instance.save(update_fields=update_fields)
      return instance
  </code></pre>