Django all-auth custom signup form with custom user model

Viewed 2262

So I have a project that uses a custom UserModel, the default UserModel is extended to use a Profile model with a OneToOne relation to the Usermodel.

I would like to use django allauth to verify emailaddresses and add social login.

I have some issues with the custom signup, I've read the following How to customize user profile when using django-allauth, but it's not entirely clear how to implement this, I get an error;

'GuestSignUpForm' object has no attribute 'save'

I've searched the forum and there seems to be no "official" documentation on how to do this, some people use ModelForms, others inherit from SignUpView, some def signup and others use def save...

I tried customizing the default UserModel with UserCreationForm and had no issues, I just need some clarification on the inner workings of the allauth package

models.py

# Custom Usermodel 
class User(AbstractUser):
    pass

# Custom Profile model to store additional info
class Profile(models.Model):
    prefix = models.CharField(_('prefix'), max_length=8, blank=True, default="")
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    # Other fields left out for readability

    def __str__(self):
        if self.prefix:
            return '%s %s, %s' % (self.user.last_name, self.prefix, self.user.first_name)
        else:
            return '%s %s' % (self.user.last_name, self.user.first_name)

# Create a profile record on save
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
    else:
        instance.profile.save()

forms.py

from django import forms
from allauth.account.forms import SetPasswordField, PasswordField

from .models import User, Profile

class GuestSignUpForm(forms.Form):
    first_name = forms.CharField(max_length=30)
    prefix = forms.CharField(max_length=8, required=False)
    last_name = forms.CharField(max_length=150)
    email = forms.EmailField(required=True)
    password1 = SetPasswordField()
    password2 = PasswordField()

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()
        profile = Profile.objects.get(user=user)
        profile.prefix = self.cleaned_data.get['prefix']
        profile.save()
        return user
0 Answers
Related