gpt4 book ai didi

python - DRF JWT 身份验证对象没有属性 'id'

转载 作者:行者123 更新时间:2023-12-05 02:08:03 25 4
gpt4 key购买 nike

我正在使用图书馆 suggested by DRF ( djangorestframework_simplejwt ),安装使用

pip install djangorestframework_simplejwt

将其添加到 settings.py

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

从导入的 View 创建了两个端点

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/ ,然后得到如下 View

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 作为 primary_key,如下所示

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',
...
}

因此,考虑到我不想将 MyUser primary_key 更改为 id,我去了安装 djangorestframework_simplejwt 的地方并搜索了声明 USER_ID_FIELD 的地方。

只在venv\Jwt\Lib\site-packages\djangorestframework_simplejwt-4.4.0.dist-info\METADATA中找到,在SIMPLE_JWT中。在那里,改为

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

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

最佳答案

我重置了在 METADATA 中所做的更改,然后转到我的项目 settings.py 文件并添加

SIMPLE_JWT = {
'USER_ID_FIELD': 'user_id'
}

关于python - DRF JWT 身份验证对象没有属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61270418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com