[Django]-How can i access QueryString values in Serializer Django Rest Framework

36👍

When using ViewSets, you can access the request in the serializer context (like you access the view). You can access the query params from this

def get_alternate_name(self, obj):
    request = self.context['request']
    print request.query_params['q']
    return 'foo'

The attribute view.kwargs contains the named arguments parsed from your url-config, so from the path-part.

9👍

According to the docs you want to use self.request.query_params

You can see it being used here

DEPRECATED:

Prior to DRF 3.0:

The usage of request.QUERY_PARAMS is used and not the lowercased request.query_params

5👍

self.context['request'].query_params

👤ramwin

Leave a comment