From django rest framework 3 pre_save, pre_delete is not available than how to manipulate and insert requested user name in any field?
<p>I am using django rest framework. I want to insert request user name in the author field of posts. But I cannot manipulate  the data before saving as the pre_save method is depreciated from drf 3 <a href="http://www.django-rest-framework.org/api-guide/generic-views/" rel="nofollow">drf generic view</a>. </p>

<pre><code>class SpoterMixin(object):

  def create(self, request, *args, **kwargs):
    data = request.data 
    data['author'] = request.user.id
    serializer = self.get_serializer(data=data)
    serializer.is_valid(raise_exception=True)
    self.perform_create(serializer)
    headers = self.get_success_headers(serializer.data)
    return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

  def update(self, request, *args, **kwargs):
    partial = kwargs.pop('partial', False)
    instance = self.get_object()
    data = request.data
    data['author'] = request.user.id # here I am manipulating and inserting the requested user 
    serializer = self.get_serializer(instance,data=data, partial=partial)
    serializer.is_valid(raise_exception=True)
    self.perform_update(serializer)
    return Response(serializer.data)


class SpotViewSet(UserRequestMixin,viewsets.ModelViewSet):

  queryset = PostModel.objects.all()
  serializer_class = SpotSerializer 
</code></pre>

<p>Is there a more efficient way of doing this as if I trying to implement all the views likes this that would increase a lot of boilerplate code.</p>

<p>So is there any alternative for the pre_save method.</p>

<p>You can use <code>perfom_create</code> and <code>perform_update</code> in your views </p>

<blockquote>
  <p>The pre_save and post_save hooks no longer exist, but are replaced
  with perform_create(self, serializer) and perform_update(self,
  serializer).</p>
  
  <p>These methods should save the object instance by calling
  serializer.save(), adding in any additional arguments as required.
  They may also perform any custom pre-save or post-save behavior.</p>
</blockquote>

<p>Example</p>

<pre><code>def perform_create(self, serializer):
    # Include the owner attribute directly, rather than from request data.
    instance = serializer.save(owner=self.request.user)
    # Perform a custom post-save action.
    send_email(instance.to_email, instance.message)
</code></pre>

python django rest python-3.x django-rest-framework