Django REST Framework: 'BasePermissionMetaclass' object is not iterable

心已入冬 提交于 2020-01-12 19:09:51

问题


Python/Django n00b moving over from javascript.

Trying to add an API endpoint using Django REST Framework which I'm hoping will ultimately be able to update a User with the body of a PATCH request, but for now I just want it to not throw a 500 error.

I've added this to urlpatterns:

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

And that should bring in this view:

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

The UserSerializer looks like this:

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

Seem to be getting this every time I visit the route:

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

I have no idea what I'm doing - anyone seen this before?

UPDATE: Full Traceback:

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

回答1:


You have mistyped the comma in DEFAULT_PERMISSION_CLASSES value, due to which Django takes it as a string, instead of a tuple.

Solution:

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



回答2:


I had the same problem, but looked for wrong place. I made mixin class with permissions and there was code

permission_classes = (
    permissions.IsAuthenticated
)

but should be

   permission_classes = (
        permissions.IsAuthenticated,
    )

So do not forget to look for other classes with permissions. I hope it will help someone.




回答3:


As a reference for other people who might be searching here, this could be an issue too..

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

Should change to:

    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]


来源:https://stackoverflow.com/questions/53649252/django-rest-framework-basepermissionmetaclass-object-is-not-iterable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!