设置的Django Django的allauth从社会登录保存extra_data信号
我使用Django 1.8.15和Django的allauth 0.28.0
说明
我想要什么这样做是很容易解释: 如果用户通过社交媒体登录(Facebook,谷歌+或Twitter)我想要从sociallogin
的extra_data
获得的URL头像和其他信息,将其存储在我的Profile
,这是我的User
模型的One-to-one relation
。
我接近
1)
首先我添加了一个类pre_social_login
像here用户已经通过社交媒体在签署后调用。
models.py
class SocialAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
"""Get called after a social login. check for data and save what you want."""
user = User.objects.get(id=request.user.id) # Error: user not available
profile = Profile(user=user)
picture = sociallogin.account.extra_data.get('picture', None)
if picture:
# ... code to save picture to profile
settings.py
SOCIALACCOUNT_ADAPTER = 'profile.models.SocialAdapter'
我想从用户的实例,但现在看来,它尚未建立。所以,我试过如下:
2)
我有创造,如果添加一个新的用户配置文件的另一个信号:
models.py
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile_for_new_user(sender, created, instance, **kwargs):
"""Signal, that creates a new profile for a new user."""
if created:
profile = Profile(user=instance)
# following code was pasted here
profile.save()
我已经添加以下:
data = SocialAccount.objects.filter(user=instance)
# do more with data
但是data
它总是空的[]
我有问题要理解。我的意思是,如果用户通过社交媒体登录因为它没有创建还那么SocialAccount
当我试图把它的情况下,2应已创建我无法从User
(情况1)访问用户?你有任何其他想法来解决这个问题吗?或者我在这里错过了什么?
在此先感谢