__init__() 接受 1 个位置参数,但 2 个被赋予 django python

__init__() takes 1 positional argument but 2 were given django python

我正在尝试列出来自 api 的用户并使用 jwt 身份验证但是当我 运行 localhost:8000/api/ 收到此错误时:

_init__() takes 1 positional argument but 2 were given

urls.py:

from django.urls import path,include
from . import views
from rest_framework_simplejwt.views import TokenObtainPairView,    TokenRefreshView




urlpatterns = [
    path('api/',views.UserViewSet, name = 'user_list'),
    path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

views.py:

class UserViewSet(viewsets.ModelViewSet):
queryset = User.object.all()
serializer_class = serializers.UserSerializers

对于 ViewSet,您需要 use .as_view() as well [drf-doc]:

The method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the .as_view() method.

在你的urlpatterns中,你需要使用.as_view():

urlpatterns = [
    path('api/',views.UserViewSet<b>.as_view()</b>, name = 'user_list'),
    path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]