How to pass a extra parameter using perform_create of django rest framwork?
<p>I am using django rest frameworks generic views. I am trying to insert request user name in the author field of post. </p>

<h1>Serializers</h1>

<pre><code>class PostSerializer(serializers.ModelSerializer):
    spoter = serializers.PrimaryKeyRelatedField(
        queryset= User.objects.all(),
        )

class Meta:
    model = PostModel
    fields = ('author','text')
</code></pre>

<h1>View</h1>

<pre><code>class UserRequestMixin(object):
    def perform_update(self, serializer):
        serializer.save(author = self.request.user.id)

    def perform_create(self, serializer):
        serializer.save(author = self.request.user.id)

class PostViewSet(UserRequestMixin,DefaultsMixin,viewsets.ModelViewSet):

    permission_classes = (IsOwnerOrReadOnly,)
    queryset = PostModel.objects.all()
    serializer_class = PostSerializer
</code></pre>

<h1>Error</h1>

<pre><code> status -&gt; 400
 {
    "author": [
      "This field is required."
     ] 
  } 
</code></pre>

<p>Depending on what you want to do, you should:</p>

<ul>
<li>Mark the author field as either <code>read_only=True</code> or <code>required=False</code></li>
<li>Set the author field with <a href="http://www.django-rest-framework.org/api-guide/validators/#createonlydefault" rel="nofollow"><code>CreateOnlyDefault</code></a></li>
</ul>

<p>For example:</p>

<pre><code>class PostSerializer(serializers.ModelSerializer):
    spoter = serializers.PrimaryKeyRelatedField(
        queryset= User.objects.all(),
    )

    class Meta:
        model = PostModel
        fields = ('author','text')
        extra_kwargs = {
            'author': {
                'read_only': True,
                'default': serializers.CurrentUserDefault(),
            }
        }
</code></pre>

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