问题描述
我收到错误.accepted_renderer没有设置响应resp api django。
I am getting the error ".accepted_renderer not set on Response resp api django".
我正在关注django rest-api教程。
Django版本我使用1.8.3
我按照教程直到第一部分。它工作正常但是当我继续发送响应的第二部分时,我收到一个错误
I am following the django rest-api tutorial.Django version i am using 1.8.3I 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.
推荐答案
您可能已将 DjangoModelPermissions
您的设置中的默认权限类。如下所示:
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
只能应用于具有 .queryset
属性或 .get_queryset()
方法的视图。
DjangoModelPermissions
can only be applied to views that have a .queryset
property or .get_queryset()
method.
由于教程2使用FBV,您可能需要将其转换为CBV,或者简单的方法是为该视图指定不同的权限类。
您必须在视图中使用 api_view
装饰器。然后,您可以定义权限
,如下所示:
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>',
...
)
}
这篇关于不能在不具有`.queryset`属性或覆盖`.get_queryset()'方法的视图上应用DjangoModelPermissions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!