1

It appears that there are multiple ways to add a simple field to a django-allauth signup form.

From @danielfeldroy I see what's quoted below.

# 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()

But then, in the response to a ticket from https://github.com/pennersr/django-allauth/issues/826 pennersr (django-allauth's founder) states that all we have to do is:

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30, label='Voornaam')
    last_name = forms.CharField(max_length=30, label='Achternaam')

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()

And then add ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm' to my settings.

See: How to customize user profile when using django-allauth

But then in the docs we have this: https://django-allauth.readthedocs.io/en/latest/forms.html#signup-allauth-account-forms-signupform

Where we do this:

from allauth.account.forms import SignupForm
class MyCustomSignupForm(SignupForm):

def save(self, request):

    # Ensure you call the parent class's save.
    # .save() returns a User object.
    user = super(MyCustomSignupForm, self).save(request)

    # Add your own processing here.

    # You must return the original result.
    return user

And we add ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'}ACCOUNT_FORMS = {'signup': 'mysite.forms.MyCustomSignupForm'} to the settings file.

So my basic question, if we assume we just want to add a couple of fields, which is the right one?

Because I have a really basic setup and none of the approaches work.

I get close with the one where we inherit the forms.Form. Well, I don't get any errors. But it doesn't actually save the input. Even though I can see the cleaned() input data through with print() statements.

I'm really confused and I hope that someone can help me sort out what is best approach.

Here's what I have.

class CustomSignupForm(forms.Form):
    opt_in = forms.BooleanField(label="Add me to the email list", help_text="Don't worry. We won't spam you.", initial=True, required=False)

    def signup(self, request, user):
        user.opt_in = self.cleaned_data['opt_in']
        user.save

And then in settings I have ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.CustomSignupForm'

Dave Merwin
  • 1,382
  • 2
  • 22
  • 44
  • Are you using fields on the `User` model (or your custom User model) or trying to use fields on a user profile model? –  Oct 16 '20 at 21:15
  • I just tried the second method again (the one where SignupForm inherits from forms.Form). It still works perfectly, so instead of showing us the examples, you should probably show your actual code. –  Oct 16 '20 at 21:41
  • I added my code to clarify. The issue is that the Boolean is not saving. It is a custom user model and t does have the `opt_in` field. – Dave Merwin Oct 16 '20 at 22:23

1 Answers1

0

So if this is your code verbatim, the error is that you're not calling the save method, because the brackets are missing:

class CustomSignupForm(forms.Form):
    opt_in = forms.BooleanField(label="Add me to the email list", help_text="Don't worry. We won't spam you.", initial=True, required=False)

    def signup(self, request, user):
        user.opt_in = self.cleaned_data['opt_in']
        user.save() # Note ()

And that should fix your problem