這裏是@pennersr解決一個具體的例子:
Assumming您的個人資料模型有這3個領域:first_name
,email
,picture_url
views.py:
@receiver(user_signed_up)
def populate_profile(sociallogin, user, **kwargs):
if sociallogin.account.provider == 'facebook':
user_data = user.socialaccount_set.filter(provider='facebook')[0].extra_data
picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large"
email = user_data['email']
first_name = user_data['first_name']
if sociallogin.account.provider == 'linkedin':
user_data = user.socialaccount_set.filter(provider='linkedin')[0].extra_data
picture_url = user_data['picture-urls']['picture-url']
email = user_data['email-address']
first_name = user_data['first-name']
if sociallogin.account.provider == 'twitter':
user_data = user.socialaccount_set.filter(provider='twitter')[0].extra_data
picture_url = user_data['profile_image_url']
picture_url = picture_url.rsplit("_", 1)[0] + "." + picture_url.rsplit(".", 1)[1]
email = user_data['email']
first_name = user_data['name'].split()[0]
user.profile.avatar_url = picture_url
user.profile.email_address = email
user.profile.first_name = first_name
user.profile.save()
如果你感到困惑中的每個供應商的picture_url
變量。然後看看文檔:
Facebook的:
picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large"
Docs
LinkedIn:
picture_url = user_data['picture-urls']['picture-url']
Docs
嘰嘰喳喳:
picture_url = picture_url.rsplit("_", 1)[0] + "." + picture_url.rsplit(".", 1)[1]
Docs而爲rsplit()
看看here
希望有所幫助。 :)
我不得不這樣用它:kwargs.get('user')。socialaccount_set.filter(provider ='facebook')。extra_data但是當我這樣做時,我得到這個錯誤:AttributeError at/accounts/facebook/login/callback/ 'QuerySet'對象沒有屬性'extra_data'謝謝你的回答!你的應用真棒 –
糟糕,我的例子是錯誤的(我剛糾正它)。我忘了從查詢集([0])中選擇一個項目,如下所示:「.filter(...)[0] ...」 – pennersr
工作正常!非常感謝! –