2016-11-21 68 views
0

我的models.py文件如下如何解決不空約束

Class Project(models.Model): 
    ABI_choices = (
    ('android-tv/x86','android-tv/x86'), 
    ('abi_2', 'google_apis/x86'), 
    ('abi_3','google_apis/x86_64'),) 
    Screen_size = (
     (1,"Landscape"), 
     (2,"Prtarit") 
    ) 
    API_level = (
     ('android-22','Lolipop'), 
     ('android-23','kitkat'), 
     ('android-25','marshmellow'), 
    ) 
    CPU_delay = (
     (1,0), 
     (2,100), 
     (3,200), 
     (4,300), 
     (5,400), 
     (6,500), 
    ) 
    Network_delay = (
     (1,20), 
     (2,30), 
     (3,40), 
     (4,50), 
     (5,60), 
    ) 
    abi = models.CharField(choices = ABI_choices,max_length=100,default=None) 
    screen = models.CharField(choices = Screen_size,max_length=100,default=None) 
    version = models.CharField(choices = API_level,max_length=100,default=None) 
    GSM = models.CharField(choices = CPU_delay,max_length=100,default=None) 
    network_delay = models.CharField(choices = Network_delay,max_length=100,default=None) 

,但是當我嘗試運行遷移它告訴我下面的錯誤

django.db.utils.IntegrityError: NOT NULL constraint failed: app_test_project__new.Network_delay

回答

2

相反的:

models.CharField(choices = ABI_choices,max_length=100,default=None) 

使用:

models.CharField(choices = ABI_choices,max_length=100,null=True, blank=True) 

適用於任何不是必填字段的字段,或者您計劃允許使用nullempty值的任何字段。

+0

它仍然不起作用:(我也是這麼做的 –

+0

然後你應該用你的改變和新的錯誤信息來更新你的問題,IntegrityError'NOT NULL'只能發生在'null = False'默認情況下),或者不是'null = True',請確保在所有字段中都改變了,我建議刪除'default = None'。 – Risadinha

相關問題