2

I have a custom Form already working for normal sign in (with email), now i have the problem with the social one.

Following the official documentation

from allauth.socialaccount.forms import SignupForm
class MyCustomSocialSignupForm(SignupForm):

    def save(self):

        # Ensure you call the parent classes save.
        # .save() returns a User object.
        user = super(MyCustomSocialSignupForm, self).save()

        # Add your own processing here.

        # You must return the original result.
        return user

And my code is the following :

settings.py

ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupFormEmail'
SOCIALACCOUNT_FORMS = {'signup': 'accounts.forms.SignupFormSocial'}

forms.py

class SignupFormEmail(forms.Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['localizacion'].label = 'Categorias'

    localidades = Alcance.objects.exclude(pk=1).filter(zona_afectada=Alcance.CONCEJO)

    localizacion = forms.ModelChoiceField(queryset=localidades, label='Concejo',empty_label="Selecciona tu concejo")

    def signup(self, request, user: User):
        self.user=user
        user.residencia.add(self.cleaned_data['localizacion'])
        user.save()



class SignupFormSocial(SignupForm):

    localidades = Alcance.objects.exclude(pk=1).filter(zona_afectada=Alcance.CONCEJO)

    localizacion = forms.ModelChoiceField(queryset=localidades, label='Concejo', empty_label="Selecciona tu concejo")

    def __init__(self, sociallogin=None, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['localizacion'].label = 'Categorias'


    def save(self,request):
        # Ensure you call the parent classes save.
        # .save() returns a User object.
        user = super(SignupFormSocial, self).save() 
        # Add your own processing here.
        user.residencia.add(self.cleaned_data['localizacion'])
        user.save()

        # You must return the original result.
        return user

The problem is that i get the following exception django.core.exceptions.ImproperlyConfigured: Error importing form class accounts.forms: "cannot import name 'BaseSignupForm'"

I also try , inheriting from form.Form and from SignupFormEmail , but none of them work

How can I do this ?

Updated with Installed apps in settings :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    # Django AllAuth
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.twitter',
    #My apps
    'accounts.apps.AccountsConfig',

1 Answers1

2

At the end I found the problem,

all auth It has by default

SOCIALACCOUNT_AUTO_SIGNUP = True

I don't know why , it was interpreting that the attribute localization was given by social account , when it was not.

So the solution was to put it to false and use the normal way to custom the form (more info here)

ACCOUNT_SIGNUP_FORM_CLASS = 'accounts.forms.SignupForm'
SOCIALACCOUNT_AUTO_SIGNUP = False