繁体   English   中英

列 user_id 不是唯一的,具有 Django UserProfiles

[英]column user_id is not unique, with Django UserProfiles

我正在尝试通过 Django(1.2.5,Ubuntu natty 中提供的版本)中的用户配置文件向用户添加一些额外的属性,但是每当我通过管理控制台创建新用户时,使用其中一个新用户包括属性(例如,“电话”),我得到一个“列 user_id 不是唯一的”IntegrityError。

我看到其他人遇到了这个问题,他们似乎已经通过向用户配置文件创建信号添加一个唯一的 dispatch_uid 来解决它,但这对我不起作用:

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

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phone = models.CharField(max_length=40,blank=True,null=True)

def create_user_profile(sender, instance, **kwargs):
        UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User, dispatch_uid="users-profilecreation-signal")

在 settings.py 中,AUTH_PROFILE_MODULE 被设置为指向这个 class。

有人知道我在做什么错吗?

有两次尝试创建相同的配置文件。 一份来自信号,一份直接来自内联管理表单。

这个问题/答案涵盖了它和更详细的潜在解决方案。

也许检查一下这是否是创建用户的信号。 您可能会看到带有许多回调的竞争条件。

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.get_or_create(user=instance)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM