85

I am getting the error ".accepted_renderer not set on Response resp api django".

I am following the django rest-api tutorial. Django version i am using 1.8.3 I followed the tutorial till first part. It worked properly. But when i continued the 2nd part in sending response, i got an error

Cannot apply DjangoModelPermissions on a view that does not have `.queryset` property or overrides the `.get_queryset()` method.

Then i tried other ways i got

.accepted_renderer not set on Response resp api django

Please help me out. I think its permission issue.

2

8 Answers 8

134

You probably have set DjangoModelPermissions as a default permission class in your settings. Something like:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.DjangoModelPermissions',
    )
}

DjangoModelPermissions can only be applied to views that have a .queryset property or .get_queryset() method.

Since Tutorial 2 uses FBVs, you probably need to convert it to a CBV or an easy way is to specify a different permission class for that view. You must be using the api_view decorator in your view. You can then define permissions like below:

from rest_framework.decorators import api_view, permission_classes
from rest_framework import permissions

@api_view([..])
@permission_classes((permissions.AllowAny,))
def my_view(request)
    ...

To resolve the renderer error, you need to add the corresponding renderer to your settings.

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.<corresponding_renderer>',
        ...
    )
}
3
  • 14
    Quick fix, comment out the 'rest_framework.renders.DjangoModelPermissions' line for now -- if you are following the DRF Tutorial 2; and perhaps you had added that in settings.py during the homepage example.
    – ProfNandaa
    Dec 1, 2015 at 19:13
  • 1
    In your second code block, you will need to import rest_framework.permissions as well. Oct 14, 2016 at 16:49
  • 1
    This is a much better answer as it allows the dev to decide on a particular permission class to use with the view. Appreciate it as this led me to the why's. Thanks!
    – Harlin
    Jun 6, 2019 at 22:53
25

I got it working in another way. My logged in user was the superuser which i have created. So i have created another user from admin and made him staff user and provided all the permissions. Then logged in to admin by that user.

In settings.py file i changed code.

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

And it worked.

1
  • Great! Actually, the issue was in the permissions. Defining permissions explicitly in the settings or via the decorator in FBV would solve the issue. Jul 10, 2015 at 10:21
10

In my case, (for tutorial 2, djangorestframework ver 3.7.7), it works when I change settings to:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ]
}
0
6

Solution for me was as pointed out by @ProfNandaa above

Quick fix, comment out the 'rest_framework.renders.DjangoModelPermissions' line for now -- if you are following the DRF Tutorial 2; and perhaps you had added that in settings.py during the homepage example.

I had indeed added this from the homepage example before embarking on the tutorial and hit the same issue.

When I commented out the offending code

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.DjangoModelPermissions',
    )
}

from settings.py it all worked fine again.

6

There are lots of good solutions already listed here. I also faced the same problem in second tutorial. It was showing:

Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set .queryset or have a .get_queryset() method.

I changed the settings.py to exclude DEFAULT_PERMISSION_CLASSES like below:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
    ]
}

Then it runs successfully. I tried this before I have found these answers.

2

From Django Rest Framework's documentation, you can add this to your view:

queryset = User.objects.none()

1

For test, you can use

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny',
    ]
}

This setting defaults to allowing unrestricted access. Documentions

0

if you're using @app_view then you have to simply remove this part:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
        'rest_framework.permissions.DjangoModelPermissions',
        'rest_framework.permissions.AllowAny',
        
    ],
    
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.