1
votes

I'm using a library suggested by DRF (djangorestframework_simplejwt), installed using

pip install djangorestframework_simplejwt

Added it to settings.py

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

Created two endpoints from imported views

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'),
]

The users are being created without any problem in the database and the passwords are being hashed.

If i go to http://localhost:8000/api/token/, then get the following view

Token Obtain Pair JWT DRF

Posting the correct user with its password and then I get the following error

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'

The error is clear, MyUser object doesn't have attribute id. Instead, it has user_id as a primary_key, as you can see next

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)

Thing is, I don't want to be changing the current MyUser fields.


EDIT

Going to the settings of djangorestframework_simplejwt it's possible to read

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

So, considering I don't want to change MyUser primary_key to id, I've gone to the place djangorestframework_simplejwt was installed and searched where USER_ID_FIELD is being declared.

Only found it in venv\Jwt\Lib\site-packages\djangorestframework_simplejwt-4.4.0.dist-info\METADATA, in SIMPLE_JWT. There, changed to

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

Then, restarted the venv and runserver. The problem still happens.

1

1 Answers

3
votes

I've reset the changes done in METADATA, then gone to my project settings.py file and added

SIMPLE_JWT = {
    'USER_ID_FIELD': 'user_id'
}