How to add additional fields on the base of inputs in django rest framework mongoengine
<p>I am developing an <code>API</code>, using <code>django-rest-framework-mongoengine</code> with <code>MongoDb</code>, I want to append additional fields to the request from the <code>serializer</code> on the base of user inputs, for example If user enters <code>keyword</code>=<code>@rohit49khatri</code>, I want to append two more fields to the request by manipulating <code>keyword</code>, like <code>type</code>=<code>username</code>, <code>username</code>=<code>rohit49khatri</code></p>

<p>Here's my code:</p>

<p><strong>Serializer</strong></p>

<pre class="lang-py prettyprint-override"><code>class SocialFeedCreateSerializer(DocumentSerializer):
    type = 'username'

    class Meta:
        model = SocialFeedSearchTerm
        fields = [
            'keyword',
            'type',
        ]
        read_only_fields = [
            'type'
        ]
</code></pre>

<p><strong>View</strong></p>

<pre class="lang-py prettyprint-override"><code>class SocialFeedCreateAPIView(CreateAPIView):
    queryset = SocialFeed.objects.all()
    serializer_class = SocialFeedCreateSerializer

    def perform_create(self, serializer):
        print(self.request.POST.get('type'))
</code></pre>

<p>But when I print <code>type</code> parameter, It gives <code>None</code></p>

<p>Please help me save some time. Thanks.</p>

<p>for the additional question: how to get <code>type</code> parameter?</p>

<pre><code># access it via `django rest framework request`
self.request.data.get('type', None)
# or via `django request`
self.request.request.POST.get('type', None)
</code></pre>

<p>for the original question:</p>

<p>situation 1) IMHO for you situation, <code>perform_create</code> can handle it:</p>

<pre><code> def perform_create(self, serializer):
     foo = self.request.data.get('foo', None)
     bar = extract_bar_from_foo(foo)
     serializer.save(
         additional_foo='abc',
         additional_bar=bar,
     )
</code></pre>

<p>situation 2) If you need to <strong>manipulate</strong> it before the data goes to serializer (so that the manipulated data will pass through serializer validation):</p>

<pre><code>class SocialFeedCreateAPIView(CreateAPIView):
    queryset = SocialFeed.objects.all()
    serializer_class = SocialFeedCreateSerializer

    def create(self, request, *args, **kwargs):
        # you can check the original snipeet in rest_framework/mixin
        # original:  serializer = self.get_serializer(data=request.data)
        request_data = self.get_create_data() if hasattr(self, 'get_create_data') else request.data
        serializer = self.get_serializer(data=request_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 get_create_data(self):
        data = self.request.data.copy()
        # manipulte your data
        data['foo'] = 'foo'
        return data
</code></pre>

<p>situation 3) If you do need to <strong>manipulate</strong> the request:
(here's just an example, you can try to find out another place to manipulate the request.)</p>

<pre><code>class SocialFeedCreateAPIView(CreateAPIView):
    queryset = SocialFeed.objects.all()
    serializer_class = SocialFeedCreateSerializer

    def initial(self, request, *args, **kwargs):
        # or any other condition you want
        if self.request.method.lower() == 'post':
            data = self.request.data
            # manipulate it 
            data['foo'] = 'foo'
            request._request.POST = data
        return super(SocialFeedCreateAPIView, self).initial(request, *args, **kwargs)
</code></pre>

python django mongodb django-rest-framework mongoengine