2

I am using DJ-REST-AUTH for user registration API. I want the first_name and last_name to show in the API endpoint however right now it is just showing the default.current API endpoint

I have tried to configure the settings and create CustomRegisterSerializer. I read that I need to have a def save(self, request) function as well and have tried to implement that as well.

main app urls.py

from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView

urlpatterns = [
    
    path('admin/', admin.site.urls),
    path('api/authentication/', include('authentication.urls')),]

settings.py

INSTALLED_APPS = \[
'rest_framework',
'rest_framework.authtoken',

    'allauth',
    'allauth.account',
    'dj_rest_auth',
    'dj_rest_auth.registration',
    
    'authentication',]

SITE_ID = 1

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

)

REST_AUTH_REGISTER_SERIALIZERS = {
'REGISTER_SERIALIZER': 'authentication.serializers.CustomRegisterSerializer'
}

# Configure allauth to use email as username

ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'username'
ACCOUNT_LOGOUT_ON_GET = True

authentication app

urls.py


from django.urls import path
from dj_rest_auth.views import (
LoginView, LogoutView, PasswordResetView, PasswordResetConfirmView
)
from .views import CustomRegisterView

urlpatterns = \[
path('register/', CustomRegisterView.as_view(), name='register'),
path('login/', LoginView.as_view(), name='login'),
path('logout/', LogoutView.as_view(), name='logout'),
path('passwordReset/', PasswordResetView.as_view(), name='password_reset'),
path('passwordResetConfirm/\<str:uidb64\>/\<str:token\>/',
PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
\]

serializers.py


from rest_framework import serializers
from allauth.account.adapter import get_adapter
from django.contrib.auth.models import User
from dj_rest_auth.registration.serializers import RegisterSerializer

class CustomRegisterSerializer(RegisterSerializer):
first_name = serializers.CharField()
last_name = serializers.CharField()

    class Meta:
        model = User
        fields = ('email', 'username', 'first_name', 'last_name', 'password1', 'password2')
        
    def get_cleaned_data(self):
        return {
            'username': self.validated_data.get('username', ''),
            'password1': self.validated_data.get('password1', ''),
            'password2': self.validated_data.get('password2', ''),
            'email': self.validated_data.get('email', ''),
            'first_name': self.validated_data.get('first_name'),
            'last_name': self.validated_data.get('last_name'),
            
        }   
        
    def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        self.cleaned_data = self.get_cleaned_data()
        user.first_name = self.cleaned_data.get('first_name')
        user.last_name = self.cleaned_data.get('last_name')
        user.save()
        adapter.save_user(request, user, self)
        return user

views.py


from dj_rest_auth.registration.views import RegisterView
from rest_framework import permissions

class CustomRegisterView(RegisterView):
permission_classes = \[permissions.AllowAny\]

Macto24
  • 79
  • 6

1 Answers1

2

I found out the answer. I needed to specify the serializer on the view via serializer_class

from dj_rest_auth.registration.views import RegisterView
from rest_framework import permissions
from .serializers import CustomRegisterSerializer

class CustomRegisterView(RegisterView):
    serializer_class = CustomRegisterSerializer
    permission_classes = [permissions.AllowAny]
Macto24
  • 79
  • 6