Home > OS >  permission() for permission in self.permission_classes -> TypeError: 'str' object is no
permission() for permission in self.permission_classes -> TypeError: 'str' object is no

Time:02-22

does anybody know why do I get this error? I set default permission to IsAuthenticated. Only register has AllowAny, to allow users to register.

Not Found: /
[21/Feb/2022 21:43:06] "GET / HTTP/1.1" 404 2278
Not Found: /account
[21/Feb/2022 21:43:11] "GET /account HTTP/1.1" 404 2317
Internal Server Error: /account/register
Traceback (most recent call last):
\venv\lib\site-packages\rest_framework\views.py", line 278, in <listcomp>
    return [permission() for permission in self.permission_classes]
TypeError: 'str' object is not callable
[21/Feb/2022 21:43:14] "GET /account/register HTTP/1.1" 500 106055

My views.py class:

@api_view(['POST'])
@permission_classes(['AllowAny'])
def registration_view(request):
    serializer = RegistrationSerializer(data=request.data)
    data = {}
    if serializer.is_valid():
        account = serializer.save()
        data['response'] = "successfully registered a new user."
        data['email'] = account.email
        data['username'] = account.username
    else:
        data = serializer.errors
    return Response(data)

I am using SessionAuthentication and IsAuthenticated as default authentication and permission in my settings.py file.

CustomUser model just inherits from AbstractUser. No addition.

CodePudding user response:

You can not pass the permission classes as strings, but as references to classes, so:

from rest_framework.permissions import AllowAny

@api_view(['POST'])
@permission_classes([AllowAny])
def registration_view(request):
    # …

but you do not need to specify AllowAny since, well, it allows any request. You can implement the view with:

@api_view(['POST'])
def registration_view(request):
    # …
  • Related