How to display User in TokenSerializer after successful login dj-rest-auth?

Issue:

UserSerializer isn’t showing up in TokenSerializer after successful login through dj-rest-auth login

What I want:

I need to put more information into TokenSerializer, in my case is User info(UserSerializer).

What I tried:

I tried the Solution from stackoverflow, but still no results gained.

My code/configuration:

user/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
    username = models.CharField(
        _('username'),
        max_length=150,
        default='',
        unique=True
    )

    def __str__(self):
        return str(self.username)

    class Meta:
        db_table = 'user'
        verbose_name = _('User')
        verbose_name_plural = _('Users')

user/serializers.py

from dj_rest_auth.models import TokenModel
from dj_rest_auth.registration.serializers import RegisterSerializer
from rest_framework import serializers

from .models import User


class UserSerializer(serializers.ModelSerializer):

    class Meta:
        fields = ('id', 'username', 'email', 'last_login', 'first_name', 'last_name')
        model = User


class UserDetailsSerializer(serializers.ModelSerializer):
    key = serializers.SerializerMethodField('is_key', read_only=True)
    user = serializers.SerializerMethodField('is_user', read_only=True)

    def is_key(self, obj):
        token = TokenModel.objects.filter(user=obj).first()
        if not token:
            token = TokenModel.objects.create(user=obj)
        return str(token)

    def is_user(self, obj):
        serializers = UserSerializer(obj)
        return serializers.data

    class Meta:
        fields = ('user', 'key')
        model = User


class UserRegistrationSerializer(RegisterSerializer):

    def custom_signup(self, request, user):
        user.save()


class TokenSerializer(serializers.ModelSerializer):
    user = UserSerializer(read_only=True)

    class Meta:
        model = TokenModel
        fields = '__all__'

my settings.py

...
INSTALLED_APPS = [
    'daphne',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blink.user',
    'blink.main',
    'blink.meeting',
    'allauth',
    'allauth.account',
    'django_filters',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_tracking',
    'dj_rest_auth',
    'allauth.socialaccount',
    'dj_rest_auth.registration',
    'corsheaders',
    'import_export',
    'drf_yasg',
    'django_celery_beat',
    'django_celery_results',
]
...

AUTH_USER_MODEL = 'user.User'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication'
    ),
    'DEFAULT_SCHEMA_CLASS': (
        'rest_framework.schemas.coreapi.AutoSchema'
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'],
    'PAGE_SIZE': 20,
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
}

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
     'allauth.account.auth_backends.AuthenticationBackend',
)

REST_AUTH_REGISTER_SERIALIZERS = {
    'REGISTER_SERIALIZER': 'blink.user.serializers.UserRegistrationSerializer',
}

REST_AUTH_SERIALIZERS = {
    'TOKEN_SERIALIZER': 'blink.user.serializers.TokenSerializer',
    'USER_DETAILS_SERIALIZER': 'blink.user.serializers.UserSerializer'
}
...

Packages version:

  • dj-rest-auth==5.0.1
  • Django==4.2
  • django-allauth==0.62.1
  • djangorestframework==3.15.1

I managed to resolve the issue, here is my Solution in case if anyone needs it.