python - Django - 用户继承了 IntegrityError

标签 python mysql django inheritance django-models

我正在尝试使用 User profile功能,所以这是我的个人资料模型:

class Nuser(models.Model):
  user = models.OneToOneField(User)
  initials = models.CharField(max_length=5)
  def __unicode__(self):
    return u'%s %s' % (self.first_name, self.last_name)

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

post_save.connect(create_user_profile, sender=User)

我的模型已正确创建,但如果我尝试使用管理界面添加用户,则会收到此错误:

IntegrityError at /admin/auth/user/add/

(1062, "Duplicate entry '3' for key 'user_id'")

我做错了什么吗?

最佳答案

该信号可能被多次注册。使用dispatch_uid argument以防止这种情况发生。

例如:

post_save.connect(create_user_profile, sender=User, dispatch_uid='autocreate_nuser')

关于python - Django - 用户继承了 IntegrityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11881391/

相关文章:

python - 用列表理解替换一周内的 for 循环

MySQL 子查询条件

mysql - SQL中基数的定义是什么

python - django 中的自定义用户仪表板

python - 如何从python中的文件中的多行中提取子字符串

python - Python 对象是否源自一个共同的父类?

python - 在 Python 中打印队列的内容

php - PHP 中的 ip2long() 是否等于 MySQL 中的 INET_ATON() 函数?

django rest 权限允许 IsAdmin 和自定义权限

python - 在 Ubuntu 12.04 上为 PostgreSQL 打开端口 5432 有困难