How to customize Signup form in Django Rest Framework with Django allauth?

Hello everyone, new Django developer here.

I’m creating an API with Django Rest Framework where Django-allauth is responsible for SIgnup Form, after everything set up I have the following Register:

However, I want to change the fields on this form and in order to achieve that I wrote the following code:

# settings.py

ACCOUNT_FORMS = {
    "signup": "user.forms.SignupForm",
}

# models.py

class User(AbstractBaseUser, PermissionsMixin):
    """Custom user model that supports using email instead of username"""
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    age = models.IntegerField()
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'

    def __str__(self):
        return self.email


# forms.py

from django import forms
from django.conf import settings
from django.utils.translation import ugettext, ugettext_lazy as _

from allauth.account.forms import SignupForm as BaseSignupForm


class SignupForm(BaseSignupForm):
    email = forms.EmailField(
        widget=forms.TextInput(
            attrs={
                'type': 'email',
                'placeholder': _('E-mail address')
            }
        )
    )

    age = forms.IntegerField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if settings.SIGNUP_EMAIL_ENTER_TWICE:
            self.fields["email2"] = forms.EmailField(
                label=_("E-mail (again)"),
                widget=forms.TextInput(
                    attrs={
                        'type': 'email',
                        'placeholder': _('E-mail address confirmation')
                    }
                )
            )

        self.fields['email'].label = ugettext("E-mail")
        self.fields['email'].required = True

        self.fields['age'].label = ugettext("age")
        self.fields['age'].required = True

        to_remove = [
            'username',
            'password1',
            'password2'  # ignored when not present
        ]
        for field in to_remove:
            if field in self.fields:
                del self.fields[field]

After implementing that, nothing changed on my Signup Form and I’m still getting the same Register in DRF. Am I missing something?

Thank you in advance.

Cheers,
Mário

Did you start your project with your custom User object, or was this one of the changes made along the way?

If the latter, my suggestion would be to review the docs on Changing to a custom user model mid-project.

(And, as a general principle, I always recommend using a name other than User when creating a custom User model. It’s not required, but I’ve found it helps avoid confusion.)

Hey Ken, thank you for your reply.

I did. And everything seems to be alright on Django Admin (Users). My only issue is that SignUp form doesn’t change :confused: