設置的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)訪問用戶?你有任何其他想法來解決這個問題嗎?或者我在這裏錯過了什麼?
在此先感謝