simpleJWT 用户没有 'id' 属性

simpleJWT user has no 'id' attribute

我正在尝试将 simplejwt 包用于我的 django rest 框架项目。我收到一个 AttributeError 说我的用户模型没有 'id' 字段,这是真的,因为我自己有一个 userID 字段并且我已将其设置为主键。

但我没有在我的代码中的任何地方引用 'id' 字段,我想知道如何解决这个问题。

这是我的代码:

#Serializers.py
class UserTokenSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        token['userID'] = user.userID
        token['username'] = user.username
        token['email'] = user.email
        token['name'] = user.name
        token['balance'] = user.balance
        token['email_activation'] = user.emailActivation
        return token

#Views.py
class UserTokenView(TokenObtainPairView):
    serializer_class = UserTokenSerializer

#models.py
class User(AbstractBaseUser, PermissionsMixin):
    userID = models.AutoField(primary_key=True)
    username = models.CharField(max_length=100, unique=True, validators=[RegexValidator(regex="^(?=[a-z0-9._]{5,20}$)(?!.*[_.]{2})[^_.].*[^_.]$")])
    email= models.EmailField(max_length=100, unique=True,  validators=[EmailValidator()])
    name = models.CharField(max_length=100)
    isSuspended = models.BooleanField(default=False)
    isAdmin = models.BooleanField(default=False)
    emailActivation = models.BooleanField(default=False)
    balance = models.IntegerField(default=0)

    objects = UserManager()

    USERNAME_FIELD = 'username'

这是我在使用令牌时遇到的错误 api:

Internal Server Error: /api/token/
Traceback (most recent call last):
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework_simplejwt\views.py", line 27, in post
    serializer.is_valid(raise_exception=True)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 227, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\serializers.py", line 429, in run_validation
    value = self.validate(value)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework_simplejwt\serializers.py", line 70, in validate
    refresh = self.get_token(self.user)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework_simplejwt\serializers.py", line 65, in get_token
    return RefreshToken.for_user(user)
  File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework_simplejwt\tokens.py", line 176, in for_user
    user_id = getattr(user, api_settings.USER_ID_FIELD)
AttributeError: 'User' object has no attribute 'id'

当您不使用用户的 id 字段时,您必须在 settings.py 中指定另一个字段,例如 here.

所以做这样的事情:

SIMPLE_JWT = {
    'USER_ID_FIELD': 'userID',
}