[Answered ]-Customizing a serializer of Djoser

1👍

Finally I inherited from UserCreatePasswordRetypeSerializer instead of UserCreateSerializer to have both re_password field and my customized serializer.
And also I deleted "USER_CREATE_PASSWORD_RETYPE": True from settings and it works properly.

0👍

There is no need to remove "USER_CREATE_PASSWORD_RETYPE": True from settings.py. You simply need to define new serializer

from djoser.serializers import UserCreatePasswordRetypeSerializer

class CustomUserCreatePasswordRetypeSerializer(UserCreatePasswordRetypeSerializer):    
    class Meta(UserCreatePasswordRetypeSerializer.Meta):
        fields = ['id', 'username', 'password', 'email', 'first_name', 'last_name']

and in settings.py

DJOSER = {
    "SERIALIZERS": {
        'user_create_password_retype': 'core.serializers.CustomUserCreatePasswordRetypeSerializer',
    }
}

as it uses user_create_password_retype serializer when "USER_CREATE_PASSWORD_RETYPE" is set to True.

0👍

 from djoser.serializers import UserCreatePasswordRetypeSerializer
    class CustomUserCreatePasswordRetypeSerializer(UserCreatePasswordRetypeSerializer):
        class Meta(UserCreatePasswordRetypeSerializer.Meta):
            fields = ['id', 'username', 'email', 'first_name', 'last_name', 'password']


DJOSER = {
    'PASSWORD_RESET_CONFIRM_URL': '#/password/reset/confirm/{uid}/{token}',
    'USERNAME_RESET_CONFIRM_URL': '#/username/reset/confirm/{uid}/{token}',
    'ACTIVATION_URL': '#/activate/{uid}/{token}',
    'SEND_ACTIVATION_EMAIL': True,

    'USER_CREATE_PASSWORD_RETYPE': True,

    'SERIALIZERS': {
        'user_create': 'core.serializers.UserCreateSerializer',
        'current_user': 'core.serializers.UserSerializer',
        'user_create_password_retype': 'core.serializers.CustomUserCreatePasswordRetypeSerializer',
    },
}

0👍

This is worked for me:

Settings Djoser:

"SERIALIZERS":{
    'user_create':'account.serializers.CustomUserCreateSerializer',
    'user':'account.serializers.CustomUserCreateSerializer',
    'user_delete': 'djoser.serializers.UserDeleteSerializer',
    'user_create_password_retype': 'account.serializers.CustomUserCreateSerializer',
    'password_reset': 'account.serializers.PasswordResetEmailSentSerializer',
},

Custom Serializer:

from djoser.serializers import SendEmailResetSerializer, UserCreatePasswordRetypeSerializer
from rest_framework import serializers
from account.models import CustomUser


class CustomUserCreateSerializer(UserCreatePasswordRetypeSerializer):
    class Meta(UserCreatePasswordRetypeSerializer.Meta):
        model = CustomUser
        fields =('id','first_name','last_name','email','phone_number','password',)

Leave a comment