2014-02-06 31 views
4

如何Django車型使用NullBooleanFieldSQLlite3, 我想存儲null作爲nullBoolean字段默認值,我該怎麼辦呢?店null作爲默認值againt空布爾場

我已經將db從BooleanField遷移到NullBooleanField,但它仍將False保存爲默認值。在代碼

用法是:

LOCATOR_YES_NO_CHOICES = ((None,''), (True,'Yes'), (False, 'No')) 
employed = models.NullBooleanField(choices=LOCATOR_YES_NO_CHOICES, 
           max_length=3, 
           blank=True, null=True, default="",) 

任何例子是有很大的幫助。

回答

8

空字符串""None

>>> "" is None 
False 

如果你想默認爲None然後寫:

employed = models.NullBooleanField(choices=LOCATOR_YES_NO_CHOICES, 
           max_length=3, 
           blank=True, null=True, default=None,) 
+0

使默認=無後,我得到「IntegrityError在/攝入量/ locator /「告訴我: 」intake_locator.employed可能不是NULL「 但該字段被定義爲NullBooleanField。 – user3269734

+0

您是否在數據庫中檢查過,是否聘用的字段將接受NULL?否則你可能需要改變表格。 – lima

+0

更改模型中的字段類型不會自動更改數據庫中的表。您將需要刪除它並再次運行'syncdb',或者使用像'south'這樣的工具進行自動模式遷移。 – lanzz