Я пытаюсь настроить разрешения для моего API модели пользователя. Моя цель - позволить кому-либо создавать, перечислять и получать пользователей, но только позволять пользователю частично обновлять себя. Однако, когда я пытаюсь позвонить в мой API, я получаю:
TypeError: 'tuple' object is not callable
views.py
class UserViewSet(viewsets.ViewSet):
def get_permissions(self):
if self.action == 'partial_update':
self.permission_classes = (IsAuthenticated, IsUpdateUser,) #Also tried with [isAuthenticated, isUpdateUser], the same goes for the other if statements
if self.action in ('list','retrieve'):
self.permission_classes = (SAFE_METHODS,)
if self.action == 'create':
self.permission_classes = (AllowAny,)
return super(self.__class__, self).get_permissions()
def list(self, request):
queryset = User.objects.all()
serializer = UserSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
queryset = User.objects.all()
user = get_object_or_404(queryset, pk=pk)
serializer = UserSerializer(user)
return Response(serializer.data)
def create(self, request, format='json'):
serializer = UserCreateSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
if user:
token = Token.objects.create(user=user)
json = serializer.data
json['token'] = token.key
return Response(json, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def partial_update(self, request, *args, **kwargs):
instance = self.queryset.get(pk=kwargs.get('pk'))
serializer = self.UserSerializer(instance, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
permissions.py
class IsUpdateUser(permissions.BasePermission):
def has_permission(self, request, view):
# can write custom code
print (view.kwargs)
try:
user = User.objects.get(
pk=view.kwargs['pk'])
except:
return False
if request.user == user:
return True
return False
Полный журнал попыток получить пользователя с pk = 1
Internal Server Error: /users/get-user/1/
Traceback (most recent call last):
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 483, in dispatch
self.initial(request, *args, **kwargs)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 401, in initial
self.check_permissions(request)
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 333, in check_permissions
for permission in self.get_permissions():
File "C:\Users\maxso\Desktop\kand\zigma\backend\user\views.py", line 38, in get_permissions
return super(self.__class__, self).get_permissions()
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 280, in get_permissions
return [permission() for permission in self.permission_classes]
File "C:\Users\maxso\Envs\zigma_env\lib\site-packages\rest_framework\views.py", line 280, in <listcomp>
return [permission() for permission in self.permission_classes]
TypeError: 'tuple' object is not callable
[18/Apr/2019 13:33:45] "GET /users/get-user/1/ HTTP/1.1" 500 18019