2

How can i prepopulate the sign up form with extra data in django-allauth after I've been connected with Facebook during registration?

In my settings I have the following

settings.py

SOCIALACCOUNT_AUTO_SIGNUP = False

Let's say I have a UserProfile model with some data related to the user.

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    gender = models.CharField(max_length=1)

If I use the following form, a non-registered user can connect with Facebook and receives a registration form to fill (an instance of UserSignupForm), where the first name and the last name are already prepopulated. How can I fill automagically the gender using the data collected from Facebook? In other words, I would like to use the gender taken from facebook extra data as initial data for the sign up form.

settings.py

ACCOUNT_SIGNUP_FORM_CLASS = 'UserSignupForm'

forms.py

class UserSignupForm(forms.ModelForm):
    first_name = forms.CharField(label=_('First name'), max_length=30)
    last_name = forms.CharField(label=_('Last name'), max_length=30)

    class Meta:
        model = UserProfile
        fields = ['first_name', 'last_name', 'gender']

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

        self.instance.user = user

        self.instance.user.save()
        self.instance.save()

It seems to me that I should change the adapter.

adapter.py

class UserProfileSocialAccountAdapter(DefaultSocialAccountAdapter):
    def populate_user(self, request, sociallogin, data):
        user = super(UserProfileSocialAccountAdapter, self).populate_user(request, sociallogin, data)

        # Take gender from social data
        # The following line is wrong for many reasons
        # (user is not saved in the database, userprofile does not exist)
        # but should give the idea
        # user.userprofile.gender = 'M'

        return user

1 Answer 1

0

I'm using a solution that doesn't use the adapter, but overrides the initialization of the signup form.

forms.py

class UserSignupForm(forms.ModelForm):
    first_name = forms.CharField(label=_('First name'), max_length=30)
    last_name = forms.CharField(label=_('Last name'), max_length=30)

    class Meta:
        model = UserProfile
        fields = ['first_name', 'last_name', 'gender']

    def __init__(self, *args, **kwargs):
        super(UserSignupForm, self).__init__(*args, **kwargs)
        if hasattr(self, 'sociallogin'):
            if 'gender' in self.sociallogin.account.extra_data:
                if self.sociallogin.account.extra_data['gender'] == 'male':
                    self.initial['gender'] = 'M'
                elif self.sociallogin.account.extra_data['gender'] == 'female':
                    self.initial['gender'] = 'F'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.