创建配置文件时出现Django IntegrityError

时间:2019-08-01 00:22:31

标签: django django-allauth django-signals

尽管我正在检查用户名在这里是唯一的,但为什么会出现完整性错误: (我还尝试了try / expect IntegretyError而不是e.count())

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

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 slugify import slugify



class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
    username = models.CharField(max_length=30, blank=True, unique=True) #should be true on signup
    name = models.CharField(max_length=30, blank=True)
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    email = models.EmailField()  #should be true on signup
    avatar_url = models.URLField(default="https://image.flaticon.com/icons/png/512/64/64572.png")


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


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

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




    from allauth.account.signals import user_logged_in, password_set, user_signed_up
    from django.db.models.signals import post_save
    from django.dispatch import receiver, Signal


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

        # picture_url = ""
        if sociallogin.account.provider == 'github':

            user_data = user.socialaccount_set.filter(provider='github')[0].extra_data
            print(user_data)
            username = user_data['login']
            avatar_url = user_data['avatar_url']
            email = user_data['email']
            name = user_data['name']
            bio =  user_data['bio']
            location = user_data['location']


        if sociallogin.account.provider == 'twitter':
            user_data = user.socialaccount_set.filter(provider='twitter')[0].extra_data
            print(user_data)
            username = user_data['screen_name']
            avatar_url = user_data['profile_image_url'].replace("_normal", "")
            email = user_data['email']
            name = user_data['name']
            bio =  user_data['description']
            location = user_data['location']

        e = Profile.objects.filter(username=username)

        if e.count() > 0:
            user.profile.username = slugify(name)
        else:
            user.profile.username = username
        #except IntegrityError:

        user.profile.avatar_url = avatar_url
        user.profile.email = email
        user.profile.name = name
        user.profile.bio = bio
        user.profile.location = location
        user.profile.save()

1 个答案:

答案 0 :(得分:0)

好吧,这行代码可能是问题所在:

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

因为每次保存用户,都会触发此信号,因此将尝试创建配置文件。而是使用注释掉的代码部分:

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

然后,只有创建了用户,才会创建配置文件。

我也不知道下面的代码是否可以工作。因为模型的save()方法没有名为commit的关键字参数。因此可能会引发错误。 老实说,您不需要该信号,因此也可以将其删除。

相关问题