美文网首页
Django You are trying to add a n

Django You are trying to add a n

作者: 盘古_创世纪 | 来源:发表于2020-03-30 12:52 被阅读0次

    因为之前数据库中已经有数据了,现在加了一个字段,这个字段没有默认值,它不知道该怎么填什么东西,所以它会报错,并且有两种解决方法: 1.在当前cmd报错状态下输入1,然后输入字符串,如果是时间,则输入timezone.now 2.在models下添加default即可

    name = models.CharField(max_length=50)

    执行:python manage.py makemirations出现以下错误:

    You are trying to add a non-nullable field 'name' to contact without a default; we can't do that (the database needs something to populate existing rows).

    Please select a fix:

     1) Provide a one-off default now (will be set on all existing rows with a null value for this column)

     2) Quit, and let me add a default in models.py

    Select an option: 

    解决方法:

    先给'name'任意初始值:name = models.CharField(max_length=50, default='abc')

    然后执行:python manage.py makemirations

    再执行:python manage.py migrate

    再将default删去,即执行:name = models.CharField(max_length=50)

    执行:python manage.py makemirations

    再执行:python manage.py migrate

    原文链接:https://blog.csdn.net/check2255/java/article/details/70338443

    相关文章

      网友评论

          本文标题:Django You are trying to add a n

          本文链接:https://www.haomeiwen.com/subject/grtvuhtx.html