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
7条答案
按热度按时间ahy6op9u1#
您在
DEFAULT_PERMISSION_CLASSES
值中输入了错误的逗号,因此Django将其视为字符串,而不是元组。0md85ypi2#
我也有同样的问题,但是找错了地方,我用权限做了mixin类,有代码
但应该是
所以别忘了寻找其他有权限的类。希望对大家有所帮助。
rseugnpd3#
作为其他可能在这里搜索的人的参考,这也可能是一个问题。
应改为:
k3fezbri4#
您必须确保逗号
vuv7lop35#
万一this也可以解决,至少解决了我的问题
cyej8jka6#
只是为了完成@ihafurr的回答。
permission_classes
应为可迭代变量,因为变量由:func:viewsets.ModelViewSet.get_permission
为(https://www.cdrf.co/3.12/rest_framework.viewsets/ModelViewSet.html):将这种方法的最后一行改为下面的代码可以克服可迭代性的要求。
Iterable
的来源3bygqnnd7#
我也遇到过同样的问题,我能够用这个简单的步骤解决
所以是的,只要把权限模块下的方法放在一个括号里,你就可以开始了,希望它能有所帮助