zoukankan      html  css  js  c++  java
  • django在model中添加字段报错

    在以下类中添加 description 字段后,

    class Colors(models.Model):
        colors = models.CharField(u'颜色', max_length=10)
        description = models.CharField(u'描述', max_length=10)
        def __str__(self):
            return self.colors
    

     

    执行以下初始化数据库的步骤,报错

    C:PycharmProjectsHelloWorld>python manage.py makemigrations
    You are trying to add a non-nullable field 'description' to colors without a def
    ault; 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: 
    

    这个可能是之前已创建了表中的一条记录,之后模型中增加了一个非空的字段,但是原来已经存在的记录没有这个值

    解决方案1:

    修改字段为允许为空  null=True

    class Colors(models.Model):
        colors = models.CharField(u'颜色', max_length=10)
        description = models.CharField(u'描述', max_length=10,null=True)
        def __str__(self):
            return self.colors
    

      

    解决方案2:

    先删除整个migrations文件夹,再执行python manage.py makemigrations,和python manage.py migrate命令

  • 相关阅读:
    4header组件开发
    3组件骨架开发
    2项目目录设计
    1项目相关的一些简介
    21AJax与Comet
    20JSON
    获取特定筛选条件下数据的四分位点
    动态长度字符串的解析拆分问题
    myBase Desktop 7无限免费试用方法
    VBA 将Excel工作簿中所有表格进行一键排版
  • 原文地址:https://www.cnblogs.com/amoyzhu/p/7591733.html
Copyright © 2011-2022 走看看