Django-Rest: how to access data if JSON or URL-encoded inside view?

I need to access some nested information inside inside a post definition. The data sent is in the following form:

    "licence": {
        "tenant_name": "tenant1",

Since I'm using Django Rest with the default parsers installed (JSONParser and FormParser) I could receive JSON or HTML form content inside the request. I'd like to keep both and don't change the default parser_classes of the view. The has different types and representations based on the content:

  • HTML-encoded: <QueryDict: {..., 'licence.tenant_name': ['tenant1']}>
  • JSON: {..., 'licence': {'tenant_name': 'tenant1'}}

To handle this I currently check on the type. Is there a better way as a general use case?

class SubscribeView(views.APIView):
    serializer_class = SubscriptionSerializer
    permission_classes = (AllowAny, )

    def post(self, request):
        serializer = self.serializer_class(

        tenant_name =['licence']['tenant_name'] if type( is dict else['licence.tenant_name']
        # perform actions on tenant_name

        status_code = status.HTTP_201_CREATED
        response = {
            'success': True,
            'status_code': status_code,
            'message': 'New subscription successfully created',
        return Response(response, status=status_code)

I didn't catch that I can just use the serializer itself! It has built in features to handle multiple data encodings.

So I can just transform this:

tenant_name =['licence']['tenant_name'] if type( is dict else['licence.tenant_name']

To this:

tenant_name = serializer.validated_data.get('licence', {}).get('tenant_name', '')
Back to Top