Django: Request object has no attribute “accepted_renderer”

If you get the error Request object has no attribute “accepted_renderer” or WSGIRequest object has no attribute “accepted_renderer” when using Django Rest Framework and attempting to add a custom BaseRenderer, then there are a few issues that may be causing this. Two of the most prevalent are:

  1. Missing pyyaml dependency

    https://github.com/encode/django-rest-framework/issues/6300

    When attempting to use the rest_framework.schemas get_schema_view, Django Rest Framework requires pyyaml


    Solution #1:

    Install pyyaml

    pip install pyyaml

    Solution #2:

    Use a version of Django Rest Framework that doesn’t require pyyaml for schema

    pip install djangorestframework==3.8.0

  2. Invalid renderers settings

When setting renderer_classes it’s important to use the api_settings from rest_framework instead of the DEFAULT_RENDERER_CLASSES from your REST_FRAMEWORK Django settings.

from rest_framework.settings import api_settings

renderer_classes = tuple(api_settings.DEFAULT_RENDERER_CLASSES) + (CSVRenderer,)

https://www.django-rest-framework.org/api-guide/settings/

Leave a comment