Python 无法在没有 `.queryset` 属性或覆盖 `.get_queryset()` 方法的视图上应用 DjangoModelPermissions

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31335736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:50:30  来源:igfitidea点击:

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

pythondjangodjango-rest-framework

提问by Gaurav

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

我收到错误“.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

我正在关注 django rest-api 教程。Django 版本我使用的是 1.8.3 我一直跟着教程直到第一部分。它工作正常。但是当我继续发送响应的第二部分时,我收到了一个错误

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.

请帮帮我。我认为它的许可问题。

采纳答案by Rahul Gupta

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

您可能已在设置中设置DjangoModelPermissions为默认权限类。就像是:

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

DjangoModelPermissionscan only be applied to views that have a .querysetproperty or .get_queryset()method.

DjangoModelPermissions只能应用于具有.queryset属性或.get_queryset()方法的视图。

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_viewdecorator in your view. You can then define permissionslike below:

由于教程 2 使用 FBV,您可能需要将其转换为 CBV,或者一种简单的方法是为该视图指定不同的权限类。您必须api_view在视图中使用装饰器。然后你可以定义permissions如下:

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

回答by Gaurav

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.

在 settings.py 文件中,我更改了代码。

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.

它奏效了。

回答by Arctic Giraffe

Solution for me was as pointed out by @ProfNandaa above

正如上面@ProfNandaa 所指出的,对我来说的解决方案

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.

快速修复,暂时注释掉“rest_framework.renders.DjangoModelPermissions”行——如果您正在关注 DRF 教程 2;也许您在主页示例中在 settings.py 中添加了它。

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.

从 settings.py 一切又正常了。

回答by arsho

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 .querysetor have a .get_queryset()method.

不能在没有设置.queryset或没有.get_queryset()方法的视图上应用 DjangoModelPermissionsOrAnonReadOnly 。

I changed the settings.pyto exclude DEFAULT_PERMISSION_CLASSESlike below:

我改变了settings.py排除DEFAULT_PERMISSION_CLASSES如下:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
    ]
}

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

然后它运行成功。在找到这些答案之前,我尝试过这个。

回答by CK.Nguyen

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

就我而言,(对于教程 2,版本djangorestframework3.7.7),当我将设置更改为:

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