Django REST 框架:“BasePermissionMetaclass”对象不可迭代

Posted

技术标签:

【中文标题】Django REST 框架:“BasePermissionMetaclass”对象不可迭代【英文标题】:Django REST Framework: 'BasePermissionMetaclass' object is not iterable 【发布时间】:2019-05-08 00:33:13 【问题描述】:

Python/Django n00b 从 javascript 迁移过来。

尝试使用 Django REST 框架添加 API 端点,我希望最终能够使用 PATCH 请求的主体更新用户,但现在我只希望它不会引发 500 错误。

我已将此添加到 urlpatterns:

url(r'update/$', views.UpdateView.as_view(), name="update_user"),

这应该带来这种观点:

from django.contrib.auth.models import User
from rest_framework.generics import UpdateAPIView
from .serializers import UserSerializer

class UpdateView(UpdateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

UserSerializer 如下所示:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'pk', 'status')

似乎每次我访问该路线时都会得到这个:

TypeError at /api/update/
'BasePermissionMetaclass' object is not iterable

我不知道自己在做什么 - 以前有人见过吗?

更新:完整追溯:

Internal Server Error: /api/update/
Traceback (most recent call last):
  File "path/to/myapp/env/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "path/to/myapp/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "path/to/myapp/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "path/to/myapp/env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "path/to/myapp/env/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 483, in dispatch
    self.initial(request, *args, **kwargs)
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 401, in initial
    self.check_permissions(request)
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 333, in check_permissions
    for permission in self.get_permissions():
  File "path/to/myapp/env/lib/python2.7/site-packages/rest_framework/views.py", line 280, in get_permissions
    return [permission() for permission in self.permission_classes]
TypeError: 'BasePermissionMetaclass' object is not iterable

【问题讨论】:

显示完整的回溯。 很可能,您定义了一个权限(为班级或通过设置)并且没有将其添加为列表 显示来自设置模块的PERMISSION_CLASSES 配置。 @SachinKukreja - 你是说这个吗? ~~~~ REST_FRAMEWORK = 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAdminUser' ), ~~~~ 【参考方案1】:

您在DEFAULT_PERMISSION_CLASSES 值中输入了错误的逗号,因此 Django 将其视为字符串,而不是元组。

解决方案

REST_FRAMEWORK = 
   ...
   'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAdminUser', ),
   ...

【讨论】:

哇——就是这样!非常感谢 - 已经坚持了几天了 - 如此简单的解决方案! 惊人的答案!谢谢!【参考方案2】:

我遇到了同样的问题,但是找错了地方。我制作了具有权限的mixin类并且有代码

permission_classes = (
    permissions.IsAuthenticated
)

但应该是

permission_classes = (
    permissions.IsAuthenticated,
#                              ^
#                         a comma here
)

所以不要忘记寻找其他具有权限的类。我希望它会对某人有所帮助。

【讨论】:

对于任何想知道更改是什么的人,您必须在权限后添加一个逗号。IsAuthenticated..我花了几分钟才弄清楚:) idk 为什么我花了 15 分钟才意识到这条评论甚至存在......【参考方案3】:

作为可能在这里搜索的其他人的参考,这也可能是一个问题..

from django.contrib.auth.models import User
from rest_framework.generics import UpdateAPIView
from .serializers import UserSerializer

class UpdateView(UpdateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = IsAuthenticated

应该改为:

    from django.contrib.auth.models import User
    from rest_framework.generics import UpdateAPIView
    from .serializers import UserSerializer

    class UpdateView(UpdateAPIView):
        queryset = User.objects.all()
        serializer_class = UserSerializer
        permission_classes = [IsAuthenticated]

【讨论】:

【参考方案4】:

Junst 万一this 也可以解决。至少解决了我的问题

【讨论】:

【参考方案5】:

只是为了完成@ihafurr 的回答。

permission_classes 应该是可迭代的,因为该变量是由 :func:viewsets.ModelViewSet.get_permission 调用的 (https://www.cdrf.co/3.12/rest_framework.viewsets/ModelViewSet.html):

def get_permissions(self):
    return [permission() for permission in self.permission_classes]

将此类方法的最后一行更改为以下内容可以克服可迭代性要求。

return [permission() for permission in self.permission_classes] if isinstance(self.permission_classes], Iterable) else [self.permission_classes]

Iterable 来自哪里

try:
    from collections.abc import Iterable  # for Python >= 3.6
except ImportError:
    from collections import Iterable

【讨论】:

【参考方案6】:
@permission_classes((AllowAny,))

您必须确保 逗号

【讨论】:

以上是关于Django REST 框架:“BasePermissionMetaclass”对象不可迭代的主要内容,如果未能解决你的问题,请参考以下文章

带有 Django 2.0 的 Django REST 框架 URL

Django REST框架 -1

django.test.client 上的 Django rest 框架导入错误

Django Rest 框架令牌认证

使用 django-allauth 和 django-rest 框架

Django rest框架认证测试