如何解决非空约束

时间:2016-11-21 13:40:17

标签: django django-models django-migrations

我的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约束失败:   app_test_project__new.Network_delay

1 个答案:

答案 0 :(得分:3)

而不是:

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

使用:

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

对于非必填字段的任何字段或您计划允许nullempty值的字段。