【发布时间】: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