I want that upon hitting the 'users/me' endpoint, I should get something like
{
"id": 10,
"email": "[email protected]",
"first_name": "test",
"last_name": null,
"is_writer": true,
"joined_on": "2024-03-03T17:54:43.629054Z"
}
Instead I only get
{
"id": 10,
"email": "[email protected]"
}
This is my serializer
from djoser.serializers import UserCreateSerializer, UserSerializer
from .models import CustomUser
class CustomUserSerializer(UserSerializer):
class Meta(UserSerializer.Meta):
fields = ('id', 'email', 'first_name', 'last_name', 'is_writer', 'joined_on')
def get_fields(self):
fields = super().get_fields()
fields['email'].read_only = True
fields['first_name'].read_only = True
fields['last_name'].read_only = True
fields['is_writer'].read_only = True
fields['joined_on'].read_only = True
return fields
# read_only_fields = ('email', 'first_name', 'last_name', 'is_writer',)
class CustomUserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
fields = ('email', 'password', 'first_name', 'last_name', 'is_writer', 'joined_on')
extra_kwargs = {
'last_name': {'required': False, 'allow_null': True},
}
In my settings file I have added these
INSTALLED_APPS = [
#DJANGO PRE-DEFINED APPS
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#APPS
'account',
#3RD PARTY LIBRARIES
'rest_framework',
'djoser',
]
.....
......
......
AUTH_USER_MODEL = 'account.CustomUser'
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
}
SIMPLE_JWT = {
'AUTH_HEADER_TYPES': ('JWT',),
'ACCESS_TOKEN_LIFETIME' : timedelta(hours=3),
}
DJOSER={
'SERIALIZERS': {
'user':'account.serializers.CustomUserSerializer',
'user_create': 'account.serializers.CustomUserCreateSerializer',
}
}
Nothing seems to work. Is there any way to do it? I need to integrate it with React, so having an endpoint that could give me the details of the logged-in user would be handy. I need help with this. If somebody can help me out here, it would be nice.