2
在注册期间我与Facebook连接后,如何预先在django-allauth中使用额外数据注册表单?用django-allauth中的社交数据预先填充自定义表格
在我的设置,我有以下
settings.py
SOCIALACCOUNT_AUTO_SIGNUP = False
比方说,我有一个UserProfile
模型与用户相关的一些数据。
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
gender = models.CharField(max_length=1)
如果我使用下面的形式中,非注册用户可以与Facebook连接并接收注册表格填写(的UserSignupForm
一个实例),其中,第一名字和姓氏已经预先填充。如何使用从Facebook收集的数据自动填充性别? 换句话说,我想使用从Facebook额外数据中获得的性别作为注册表单的初始数据。
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()
在我看来,我应该更改适配器。
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