Skip to content

Instantly share code, notes, and snippets.

@Tobi-De
Created December 6, 2020 06:19
Show Gist options
  • Save Tobi-De/f304d56bdeb762403eae77e2cdbc05bc to your computer and use it in GitHub Desktop.
Save Tobi-De/f304d56bdeb762403eae77e2cdbc05bc to your computer and use it in GitHub Desktop.
Cutomize django allauth signup form, from https://github.com/pydanny/multiple-user-types-django
from django import forms as d_forms
from allauth.account.forms import SignupForm
# SpyBookSignupForm inherits from django-allauth's SignupForm
class SpyBookSignupForm(SignupForm):
# Specify a choice field that matches the choice field on our user model
type = d_forms.ChoiceField(choices=[("SPY", "Spy"), ("DRIVER", "Driver")])
# Override the init method
def __init__(self, *args, **kwargs):
# Call the init of the parent class
super().__init__(*args, **kwargs)
# Remove autofocus because it is in the wrong place
del self.fields["username"].widget.attrs["autofocus"]
# Put in custom signup logic
def custom_signup(self, request, user):
# Set the user's type from the form reponse
user.type = self.cleaned_data["type"]
# Save the user's type to their database record
user.save()
ACCOUNT_FORMS = {
"signup": "spybook.users.forms.SpyBookSignupForm",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment