Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>',
        ...
    )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...