简体   繁体   中英

Prepopulate custom form with social data in django-allauth

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM