我不能挽救一个Facebook帐户的图片链接

问题描述 投票:0回答:1

我试图得到一个Facebook帐户的图片链接,但得到这个消息:

django.db.utils.IntegrityError: UNIQUE constraint failed: 
user_profile.user_id

我可以看到在控制台中的图片链接,但我不能把它保存在用户配置文件。

这里是当我尝试这样做,我model.py。

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

from allauth.account.signals import user_signed_up, user_logged_in


from allauth.socialaccount.models import SocialAccount
import hashlib


try:
    from django.utils.encoding import force_text
except ImportError:
    from django.utils.encoding import force_unicode as force_text


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, 
    related_name='userprofile')

    city = models.CharField(max_length=30, blank=True)
    about = models.TextField(blank=True)
    avatar = models.ImageField(upload_to='avatars/', verbose_name='Images', 
    blank=True)

    sound = models.BooleanField(default=False)
    points = models.DecimalField(max_digits=4, decimal_places=2, default=0.00)
    energy = models.IntegerField(default=0)
    avatar_url = models.URLField(max_length=500, blank=True, null=True)

    class Meta:
        db_table = 'user_profile'
        verbose_name = 'Profile'
        verbose_name_plural = 'Profiles'

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

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

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.userprofile.save()


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

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

        profile = UserProfile(user=user, avatar_url=picture_url)
        #profile = UserProfile.objects.get(user=user)
        #profile.avatar_url = picture_url
        profile.save()

如果我做这样的结尾:

    #profile = UserProfile(user=user, avatar_url=picture_url)
    profile = UserProfile.objects.get(user=user)
    profile.avatar_url = picture_url
    profile.save()

我不是在控制台中得到任何消息,但用户配置文件没有保存。

django
1个回答
1
投票

当你试图创建一个已经存在的个人资料的新实例,此行profile = UserProfile(user=user, avatar_url=picture_url)引起的问题。轮廓变得因为在你OneToOne模型UserProfile领域的独特。

而且你也不需要从数据库的用户,因为set_initial_user_names功能注册用户已经传递到你作为参数。所以只是做user.userprofile。然后,你可以用新信息更新用户。

此外,我建议你下载从所提供的图片,然后将其保存在你的模型像这样的图像领域:

 import urllib
 from django.core.files import File 

 # for python 2: result = urllib.urlretrieve(picture_url)[0]      
 result = urllib.request.urlretrieve(picture_url)[0] # for python 3
 user.userprofile.avatar.save('test.jpg', File(open(result, 'rb')))
 user.userprofile.save()
© www.soinside.com 2019 - 2024. All rights reserved.