在/ accounts / google / login / callback / User没有userprofile
的RelatedObjectDoesNotExistuserprofile 是扩展用户模型(一对一关系)的模型,并在注册发生时为每个用户创建。看起来没有为Google注册创建userprofile模型,但我无法理解为什么..
我的代码是:
*views.py*
# This view creates and populates profile with social data
@receiver(user_signed_up)
def populate_profile(request, user, sociallogin=None, **kwargs):
if sociallogin:
if sociallogin.account.provider == 'facebook':
if not user.first_name and sociallogin.account.extra_data['first_name']:
user.first_name = sociallogin.account.extra_data['first_name']
if not user.last_name and sociallogin.account.extra_data['last_name']:
user.last_name = sociallogin.account.extra_data['last_name']
if not user.email and sociallogin.account.extra_data['email']:
user.last_name = sociallogin.account.extra_data['email']
user.save()
try:
userprofile = sociallogin.user.userprofile
except:
userprofile = UserProfile(user=sociallogin.user)
userprofile.facebook = sociallogin.account.get_profile_url()
userprofile.save()
if sociallogin.account.provider == 'twitter':
if not user.first_name and sociallogin.account.extra_data['name']:
name = sociallogin.account.extra_data['name']
user.first_name = name.split()[0]
if not user.last_name and sociallogin.account.extra_data['name']:
name = sociallogin.account.extra_data['name']
user.last_name = name.split()[1]
if not user.email and sociallogin.account.extra_data['email']:
user.last_name = sociallogin.account.extra_data['email']
user.save()
try:
userprofile = sociallogin.user.userprofile
except:
userprofile = UserProfile(user=sociallogin.user)
userprofile.twitter = sociallogin.account.get_profile_url()
userprofile.save()
if sociallogin.account.provider == 'google':
if not user.first_name and sociallogin.account.extra_data['given_name']:
user.first_name = sociallogin.account.extra_data['given_name']
if not user.last_name and sociallogin.account.extra_data['family_name']:
user.last_name = sociallogin.account.extra_data['family_name']
if not user.email and sociallogin.account.extra_data['email']:
user.last_name = sociallogin.account.extra_data['email']
user.save()
try:
userprofile = sociallogin.user.userprofile
except:
userprofile = UserProfile(user=sociallogin.user)
userprofile.google = sociallogin.account.get_profile_url()
userprofile.save()
*my_adapter.py*
from allauth.account.models import EmailAddress
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.contrib.auth.models import User
from user_profile.models import UserProfile
# This adapter creates an account or connects the social account to an existing account
class SocialAccountAdapter(DefaultSocialAccountAdapter):
def pre_social_login(self, request, sociallogin):
user = sociallogin.user
perfil = UserProfile(user=sociallogin.user)
perfil.save()
# Ignore existing social accounts, just do this stuff for new ones
if sociallogin.is_existing:
return
# some social logins don't have an email address, e.g. facebook accounts
# with mobile numbers only, but allauth takes care of this case so just
# ignore it
if 'email' not in sociallogin.account.extra_data:
return
# check if given email address already exists.
# Note: __iexact is used to ignore cases
try:
email = sociallogin.account.extra_data['email'].lower()
existing_user = User.objects.get(email__iexact=email)
perfil = existing_user.userprofile
email_address = existing_user.email
#email_address = EmailAddress.objects.get(email__iexact=email)
# if it does not, let allauth take care of this new social account
except User.DoesNotExist:
return
# if it does, connect this new social login to the existing user
user = existing_user
sociallogin.connect(request, user)