title: Django REST Framework - Define extra arguments using the @action decorator
body: <p>I'm wondering if would be possible to define extra arguments using the <code>action</code> decorator:</p>
  
  <pre><code>class UserViewSet(viewsets.ModelViewSet):
      """
      A viewset that provides the standard actions
      """
      queryset = User.objects.all()
      serializer_class = UserSerializer
  
      @action(methods=['post'], detail=True)
      def follow(self, request, pk=None):
          user = self.get_object()
          target_user = ???
          Follow.objects.create(user=user, target=target_user)
          return Response(status=status.HTTP_204_NO_CONTENT)
  </code></pre>
  
  <p>What I want to achieve is matching the following path: <code>api/users/{id}/follow/{target_id}</code></p>
  
  <p>The <code>follow</code> action will be used to let an user with ID <code>id</code> following another user with ID <code>target_id</code>.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Since it seems like it's not possible to pass extra args, right now I'm POSTing data as a list of user IDs to be followed:</p>
  
  <p>The serializer to validate data:</p>
  
  <pre><code>class FollowSerializer(serializers.ModelSerializer):
      user_ids = serializers.ListField(child=serializers.IntegerField(min_value=1), required=False, help_text='User target IDs')
  </code></pre>
  
  <p>The action:</p>
  
  <pre><code>    @action(detail=True, methods=['post'])
      def follow(self, request, pk=None):
          user = self.get_object()
          serializer = FollowSerializer(data=request.data)
          if serializer.is_valid():
              serializer.data['user_ids']
              for user_id in user_ids:
                  target_user = User.objects.get(pk=user_id)
                  Follow.objects.create(user=user, target=target_user)
              return Response(status=status.HTTP_204_NO_CONTENT)
          else:
              return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>The problem with my solution is that on the auto-generated doc page I'm seeing the form with the <code>UserSerializer</code> fields.</p>
  
  <p><a href="https://i.stack.imgur.com/mBpyw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mBpyw.png" alt="API Doc Page"></a></p>
  
  <p><strong>UPDATE 2</strong>:</p>
  
  <p>The following <em>trick</em> let the auto-generated doc page shows the right form:</p>
  
  <pre><code>class UserViewSet(viewsets.ModelViewSet):
      """
      A viewset that provides the standard actions
      """
      queryset = User.objects.all()
  
      def get_serializer_class(self):
          if self.action == 'follow':
              return FollowSerializer
          else:
              return UserSerializer
  
      @action(methods=['post'], detail=True)
      def follow(self, request, pk=None):
          ...
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KGOTw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KGOTw.png" alt="API Doc Page - Correct Form"></a></p>