ubuntu rooney
ubuntu rooney

Reputation: 23

Django Ordering Fields in django allauth

I am trying to order the fields to be in the order of 'full name', 'email' and then 'password' but right now it is 'email', 'full_name' and then 'password'.

Here is the image

I tried adding this to my signUp form but it didn't work:

if 'keyOrder' in self.fields:
     self.fields.keyOrder = ['full_name', 'email', 'password'] 

Here are the rest of my files:

forms.py

from allauth.account.forms import SignupForm
from django.contrib.auth.forms import UserCreationForm, UserChangeForm

from django import forms

from .models import CustomUser


class CustomSignupForm(SignupForm):
    full_name = forms.CharField(max_length=50, label='Full Name')
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['full_name'].widget.attrs.update({'autofocus': 'autofocus'})
        
        # Doesn't work
        if 'keyOrder' in self.fields:
            self.fields.keyOrder = ['full_name', 'email', 'password'] 
       
        for fieldname in ['password1']:
            self.fields[fieldname].help_text = "Your password"

adapters.py

Here, I am saving the full name to customuser model

from allauth.account.adapter import DefaultAccountAdapter
from django import forms

class UserAccountAdapter(DefaultAccountAdapter):
    
    def save_user(self, request, user, form, commit=True):
        """
        This is called when saving user via allauth registration.
        We override this to set additional data on user object.
        """
        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.full_name = form.cleaned_data.get('full_name')
        user.save()

in settings.py These are the django all-auth configuration I set at the bottom of my settings file

AUTH_USER_MODEL = 'accounts.CustomUser' 


# django-allauth config
LOGIN_REDIRECT_URL = 'home'
ACCOUNT_LOGOUT_REDIRECT = 'home' # new
SITE_ID = 1 # new
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend', # new
)
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' # new
ACCOUNT_SESSION_REMEMBER = True # new
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False # new
ACCOUNT_USERNAME_REQUIRED = False # new
ACCOUNT_AUTHENTICATION_METHOD = 'email' # new
ACCOUNT_EMAIL_REQUIRED = True # new
ACCOUNT_UNIQUE_EMAIL = True # new

ACCOUNT_FORMS = {
'signup': 'accounts.forms.CustomSignupForm',
}
ACCOUNT_ADAPTER = 'accounts.adapter.UserAccountAdapter'
# ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300
ACCOUNT_LOGIN_ON_PASSWORD_RESET = True

models.py These is the customuser model I made. I made sure to write this before I migrated anything

from django.contrib.auth.models import AbstractUser
from django.db import models
from uuid import uuid4

# Create your models here.
class CustomUser(AbstractUser):
    id = models.UUIDField( # new
        primary_key=True,
        default=uuid4,
        editable=False,
        max_length=70)
    full_name = models.CharField(max_length=75, blank=True)
    bio = models.TextField(max_length=250, blank=True)

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

Upvotes: 2

Views: 472

Answers (1)

iklinac
iklinac

Reputation: 15738

You can set field_order attribute to override default_field_order ( from BaseSignupForm source)

   set_form_field_order(
        self,
        getattr(self, 'field_order', None) or default_field_order)

So in your case something like

class CustomSignupForm(SignupForm):
    ...
    field_order = ['full_name', 'email', 'password'] 
    ...

Upvotes: 2

Related Questions