UNIQUE 约束失败:auth_profile.user_id Django Allauth

UNIQUE constraint failed: auth_profile.user_id Django Allauth

我对用户模型有一个 create_or_update_user_profile 方法,每当发生保存事件时,但我添加了一个新方法 set_initial_user_names 来保存一个 url,现在它显示给我错误 IntegrityError at /accounts/social/signup/ UNIQUE constraint failed: auth_profile.user_id

class UserProfile(models.Model):

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=170, blank=True)
    avatar_url = models.CharField(max_length=256, blank=True)
    facebook_url = models.CharField(max_length=40, blank=True)
    twitter_url = models.CharField(max_length=40, blank=True)
    instagram_url = models.CharField(max_length=40, blank=True)
    web_url = models.CharField(max_length=40, blank=True)

    class Meta():
        db_table = 'auth_profile'

    def __str__(self):
        return self.user.username


@receiver(user_signed_up)
def set_initial_user_names(request, user, sociallogin=None, **kwargs):

    preferred_avatar_size_pixels = 256

    picture_url = "http://www.gravatar.com/avatar/{0}?s={1}".format(
        hashlib.md5(user.email.encode('UTF-8')).hexdigest(),
        preferred_avatar_size_pixels
    )

    if sociallogin:
        if sociallogin.account.provider == 'twitter':
            name = sociallogin.account.extra_data['name']
            user.first_name = name.split()[0]
            user.last_name = name.split()[1]

        if sociallogin.account.provider == 'facebook':
            user.first_name = sociallogin.account.extra_data['first_name']
            user.last_name = sociallogin.account.extra_data['last_name']

            picture_url = "http://graph.facebook.com/{0}/picture?width={1}&height={1}".format(
                sociallogin.account.uid, preferred_avatar_size_pixels)

        if sociallogin.account.provider == 'google':
            user.first_name = sociallogin.account.extra_data['given_name']
            user.last_name = sociallogin.account.extra_data['family_name']
            picture_url = sociallogin.account.extra_data['picture']


    profile = UserProfile(user=user, avatar_url=picture_url)
    profile.save()


@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
    instance.userprofile.save()

IntegrityError at /accounts/social/signup/ UNIQUE constraint failed: auth_profile.user_id

您正在尝试为同一用户创建两次用户。

第一个是在创建用户实例后立即在 create_or_update_user_profile 处理程序中创建的。第二个在这里:

@receiver(user_signed_up)
def set_initial_user_names(request, user, sociallogin=None, **kwargs):
    ...

    profile = UserProfile(user=user, avatar_url=picture_url)  
    profile.save()  # <--- HERE

receiver(user_signed_up) 在用户对象已创建后触发,因此用户配置文件已由 create_or_update_user_profile 创建。

因此,要解决此问题,您应该使用现有的用户个人资料,而不是创建新的:

@receiver(user_signed_up)
def set_initial_user_names(request, user, sociallogin=None, **kwargs):
    ...

    user.userprofile.avatar_url = picture_url 
    user.userprofile.save()