【问题标题】:column user_id is not unique, with Django UserProfiles列 user_id 不是唯一的,使用 Django UserProfiles
【发布时间】:2011-08-17 00:02:50
【问题描述】:

我正在尝试通过 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")

AUTH_PROFILE_MODULE 设置为指向此类,在 settings.py 中。

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

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    question/answer 对此进行了更详细的介绍以及潜在的解决方案。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 嗯,同样的错误。恐怕我不知道这是否意味着这是一个比赛条件......干杯。
      猜你喜欢
      • 2012-11-03
      • 1970-01-01
      • 2013-08-22
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多