【问题标题】:Django - createsuperuser IntegrityErrorDjango - 创建超级用户 IntegrityError
【发布时间】:2012-10-12 15:22:22
【问题描述】:

当我尝试从 shell 或通过 manage.py 或从 Django-Admin 页面创建超级用户时;它会抛出类似的错误:

IntegrityError at /admin/auth/user/add/
null value in column "link_karma" violates not-null constraint

这是我的 models.py [用户部分]:

class User_Profile(models.Model):

    user = models.OneToOneField(User)
    link_karma = models.IntegerField()
    comment_karma = models.IntegerField()
    avatar = models.CharField(max_length=100)

    def create_user_profile(sender,instance,created,**kwargs):
        if created:
            User_Profile.objects.create(user=instance)
    post_save.connect(create_user_profile, sender=User)

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

我在 settings.py 中添加了闲置线:

AUTH_PROFILE_MODULE = 'accounts.User_Profile'

在添加此行之前;是一样的。

注意:我尝试将 link_karma 字段的类型更改为 CharField。是一样的。

注意:如果需要任何其他数据;我可以添加它。

【问题讨论】:

  • 修复代码中的缩进。顺便说一句,User 已经有电子邮件字段。
  • 我认为,create_user_profile 应该是单独的独立函数,而不是User_Profile 方法。
  • 独立函数是什么意思?
  • 不属于User_Profile

标签: django django-models django-authentication


【解决方案1】:

尝试为link_karmacomment_karma 设置blank=True, null=True。您没有将任何值传递给这些字段,因此当您的 create 语句尝试创建对象时,它不知道要给 link_karm 什么,因此会出现错误。

正如 goliney 正确指出的那样,您的 User_Profile 模型中不需要电子邮件,因为用户已经拥有它。

【讨论】:

  • 不知道我必须为 IntegerField 设置一些东西。谢谢。
猜你喜欢
  • 2021-10-13
  • 2012-12-13
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 2015-02-17
  • 2021-08-19
相关资源
最近更新 更多