django django-allauth在信号中保存来自社交登录的extra_data [英] django django-allauth save extra_data from social login in signal

查看:95
本文介绍了django django-allauth在信号中保存来自社交登录的extra_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置

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

I'm using Django 1.8.15 and django-allauth 0.28.0

说明

我想做的事情很容易解释:
如果用户通过社交媒体(facebook,google +或twitter),我想从 sociallogin 检索 extra_data 以获取头像的网址以及其他信息以将其存储在我的个人资料中,这是与我<$ c的一对一关系 $ c>用户模型。

What I want to do is pretty easy to explain: If a user logs in via social media (facebook, google+ or twitter) i want to retrieve the extra_data from sociallogin to get the url to the avatar and other information to store it in my Profile, which is a One-to-one relation to my User model.

我的方法

1)

首先,我添加了带有 pre_social_login 的类,例如此处在用户通过社交媒体登录后被调用。

First I added a class with pre_social_login like here which gets called after the user has signed in via social media.

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'

我想从用户,但似乎尚未创建,因此我尝试了以下操作:

I want to get the instance from the user but it seems, that it wasn't created yet. So I tried the following:

2)

如果添加了新用户,我还有另一个信号可以创建配置文件:

I have another signal which creates a profile if a new user is added:

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

数据总是空的[]

我很难理解。我的意思是,如果用户通过社交媒体登录,但由于尚未创建,则无法从 User (案例1)访问该用户,则在第2种情况下尝试获取该帐户时,应该已经创建了SocialAccount !您还有其他想法可以解决吗?还是我在这里错过了什么?

I have problems to understand that. I mean if a user logs in via social media and I can't access the user from User (case 1) because it wasn't created yet then the SocialAccountshould have been created when I try to get it in case 2?! Do you have any other ideas to solve that? Or am I missing something here?

预先感谢

推荐答案

几小时无奈之后得到了它。

got it after some hours of desperation.

我刚刚使用了另一个信号 user_signed_up ,而不是来自社交帐户

I've just used another signal user_signed_up, not the one from the socialaccount

from allauth.account.signals import user_signed_up

@receiver(user_signed_up)
def retrieve_social_data(request, user, **kwargs):
    """Signal, that gets extra data from sociallogin and put it to profile."""
    # get the profile where i want to store the extra_data
    profile = Profile(user=user)
    # in this signal I can retrieve the obj from SocialAccount
    data = SocialAccount.objects.filter(user=user, provider='facebook')
    # check if the user has signed up via social media
    if data:
        picture = data[0].get_avatar_url()
        if picture:
            # custom def to save the pic in the profile
            save_image_from_url(model=profile, url=picture)
        profile.save()

http://django-allauth.readthedocs.io/en/latest/ signal.html#allauth-帐户

这篇关于django django-allauth在信号中保存来自社交登录的extra_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆