2012-07-05 43 views
4
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 

當我離開任何integerfields的空白,我不斷收到一個錯誤,指出,完整性錯誤 - integerfield不能爲null

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

我想通過把空白=真和空=真,它會解決這個問題?

+0

您是否更新了數據庫以允許該字段在數據庫級別爲空? – imm

+0

使用__python manage.py sqlall __進行檢查,然後在爲孵化器創建表列表中進行驗證,但是您的整數字段沒有「NOT NULL」約束。 – Rohan

+0

對於任何人來到這個問題尋找如何設置IntegerField(空= True)爲NULL:http://stackoverflow.com/a/6291171/2271127基本上使用無。數據庫會將None轉換爲NULL。 – Gadget

回答

7

這是因爲我在創建數據庫後添加了「blank = true」和「null = true」。我不得不刪除我的數據庫,然後「syncdb」,然後它的工作。

+3

使用南:http://south.aeracode.org/,你不必刪除數據庫。 – SColvin

+3

或在Django 1.7中,現在有內置的遷移 –

0

我碰到這個職位有類似的問題,所以我想我會分享,對我工作的解決方案:

在相關models.py文件中的原始類,而不是僅僅使用構造沒有像這樣的任何值:

some_field = models.BooleanField() 

..我代替配置的默認值false:

some_field = models.BooleanField(default = False) 

..然後再次同步數據庫後,我的問題得到解決。