如何使用Django-Allauth从URL保存图像?

时间:2019-05-26 08:31:34

标签: python django django-allauth

该用户正在通过Django-AllAuth进行注册,并且其个人资料图片网址已被提取。然后我想将此图像保存到ImageField。我怎么做?这是我的代码:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(default=None, max_length=255)
    email = models.CharField(default=None, max_length=500)
    picture = models.ImageField(default='default.jpg', upload_to='profile_pics', max_length=255)

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

    @receiver(user_signed_up)
    def populate_profile(sociallogin, user, **kwargs):

        profile = Profile(user=user)

        if sociallogin.account.provider == 'google':
            user_data = user.socialaccount_set.filter(provider='google')[0].extra_data
            picture_url = user_data['picture']
            email = user_data['email']
            full_name = user_data['name']

        profile.picture = picture_url
        profile.email = email
        profile.full_name = full_name
        profile.save()

0 个答案:

没有答案