小编Kro*_*nig的帖子

django django-allauth 在信号中保存来自社交登录的 extra_data

设置

我正在使用 Django 1.8.15 和 django-allauth 0.28.0

描述

我想要做的很容易解释:如果用户通过社交媒体(facebook、google+ 或 twitter)登录,我想检索extra_datafromsociallogin以获取头像的 url 和其他信息以将其存储在我的Profile.一One-to-one relation到我的User模型。

我的方法

1)

首先,我添加了一个pre_social_login类似here的类,它在用户通过社交媒体登录后被调用。

模型.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 
Run Code Online (Sandbox Code Playgroud)

设置.py

SOCIALACCOUNT_ADAPTER = …
Run Code Online (Sandbox Code Playgroud)

python django django-allauth

3
推荐指数
1
解决办法
1917
查看次数

标签 统计

django ×1

django-allauth ×1

python ×1