DRF JWT身份验证对象没有属性“id”

2024-07-08 15:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个库suggested by DRFdjangorestframework_simplejwt),使用

pip install djangorestframework_simplejwt

将其添加到settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ]
}

从导入的视图创建了两个端点

from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView

urlpatterns = [
    # JWT Token
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain'),
    # get a new token before the old expires.
    path('api/token/refresh/', TokenRefreshView.as_view, name='token_refresh'),
]

用户的创建在数据库中没有任何问题,密码正在散列

如果我转到http://localhost:8000/api/token/,那么得到以下视图

Token Obtain Pair JWT DRF

发布正确的用户及其密码,然后我得到以下错误

AttributeError at /api/token/

[17/Apr/2020 12:06:51] "POST /api/token/ HTTP/1.1" 500 122221 Internal Server Error: /api/token/ Traceback (most recent call last): File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\django\views\generic\base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\views.py", line 505, in dispatch response = self.handle_exception(exc) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception raise exc File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\views.py", line 502, in dispatch response = handler(request, *args, **kwargs) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework_simplejwt\views.py", line 27, in post serializer.is_valid(raise_exception=True) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\serializers.py", line 234, in is_valid self._validated_data = self.run_validation(self.initial_data) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework\serializers.py", line 436, in run_validation value = self.validate(value) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework_simplejwt\serializers.py", line 73, in validate refresh = self.get_token(self.user) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework_simplejwt\serializers.py", line 68, in get_token return RefreshToken.for_user(user) File "C:\Users\tiago\Desktop\letsgo\authenticationJwt\lib\site-packages\rest_framework_simplejwt\tokens.py", line 161, in for_user user_id = getattr(user, api_settings.USER_ID_FIELD) AttributeError: 'MyUser' object has no attribute 'id'

错误很明显,MyUser对象没有属性id。相反,它将user_id作为主键,如下面所示

class MyUser(AbstractBaseUser):
    user_id = models.AutoField(primary_key=True, db_column='userId')
    password = models.CharField(db_column='userPassword', max_length=256)
    email = models.EmailField(verbose_name='email', max_length=100, unique=True)

问题是,我不想更改当前的MyUser字段


编辑

settings of djangorestframework_simplejwt可以阅读

DEFAULTS = {
    ...
    'USER_ID_FIELD': 'id',
    ...
}

因此,考虑到我不想将我的USER primary_key更改为id,我去了安装djangorestframework_simplejwt并搜索USER_id_字段的地方

仅在venv\Jwt\Lib\site packages\djangorestframework\u simplejwt-4.4.0.dist info\METADATA中的SIMPLE\u Jwt中找到它。好了,换成

  SIMPLE_JWT = {
      ...
      'USER_ID_FIELD': 'user_id',
      ...
  }

然后,重新启动venv和runserver。问题仍然存在


Tags: inpyrestlibpackageslinesiteframework
1条回答
网友
1楼 · 发布于 2024-07-08 15:33:58

我已经重置了元数据中所做的更改,然后转到我的project settings.py文件并添加了

SIMPLE_JWT = {
    'USER_ID_FIELD': 'user_id'
}

相关问题 更多 >

    热门问题