Setup
I'm using Django 1.8.15 and django-allauth 0.28.0
Description
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.
My approaches
1)
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()
I've added the following:
data = SocialAccount.objects.filter(user=instance)
# do more with data
but data it is always empty []
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?
Thanks in advance