【问题标题】:integrity error - integerfield may not be null完整性错误 - integerfield 可能不为空
【发布时间】:2012-07-05 02:15:47
【问题描述】:
class Incubator(models.Model):
    name = models.CharField(max_length=30)
    category = models.CharField(max_length=30, blank=True)
    acceptanceRate = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
    funding = models.IntegerField()
    programLength = models.DecimalField(max_digits=4, decimal_places=2)
    kloutScore = models.IntegerField(blank=True, null=True)
    companies = models.ManyToManyField(Company, blank=True)

    def __unicode__(self):
        return self.name

当我将任何整数字段留空时,我不断收到一条错误消息,提示:

IntegrityError at /admin/incubators/incubator/add/
incubators_incubator.kloutScore may not be NULL

我想通过设置blank=true和null=true,就可以解决这个问题了?

【问题讨论】:

  • 您是否更新了数据库以允许该字段在数据库级别可以为空?
  • 检查 python manage.py sqlall 并在为 Incubator 创建表列表中验证,您的整数字段没有“NOT NULL”约束。
  • 对于任何来这个问题寻找如何将 IntegerField(null=True) 设置为 NULL 的人:stackoverflow.com/a/6291171/2271127 基本上使用无。数据库将为您将 None 转换为 NULL。

标签: django


【解决方案1】:

这是因为我在创建数据库之后添加了“blank=true”和“null=true”。我不得不删除我的数据库,然后删除“syncdb”,然后它就起作用了。

【讨论】:

  • 或在 Django 1.7 中,现在有内置迁移
【解决方案2】:

我在这篇文章中遇到了类似的问题,所以我想我会分享对我有用的解决方案:

在相关models.py文件中的原始类中,而不是仅仅使用没有任何值的构造函数:

 some_field = models.BooleanField()

..我改为配置了一个默认值False:

 some_field = models.BooleanField(default = False)

..然后再次同步数据库后,我的问题就解决了。

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多