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

zbq4xfa0  于 2023-01-31  发布在  Go
关注(0)|答案(7)|浏览(265)

Python/Django n 00 b从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
ahy6op9u

ahy6op9u1#

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

    • 溶液**:
REST_FRAMEWORK = {
   ...
   'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAdminUser', ),
   ...
}
0md85ypi

0md85ypi2#

我也有同样的问题,但是找错了地方,我用权限做了mixin类,有代码

permission_classes = (
    permissions.IsAuthenticated
)

但应该是

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

所以别忘了寻找其他有权限的类。希望对大家有所帮助。

rseugnpd

rseugnpd3#

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

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]
k3fezbri

k3fezbri4#

@permission_classes((AllowAny,))

您必须确保逗号

vuv7lop3

vuv7lop35#

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

cyej8jka

cyej8jka6#

只是为了完成@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
3bygqnnd

3bygqnnd7#

我也遇到过同样的问题,我能够用这个简单的步骤解决

@permission_classes([IsAuthenticated])

所以是的,只要把权限模块下的方法放在一个括号里,你就可以开始了,希望它能有所帮助

相关问题