Created
December 6, 2020 06:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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