在以下类中添加 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:\PycharmProjects\HelloWorld>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命令

django在model中添加字段报错的更多相关文章

  1. Django的model中日期字段设置默认值的问题

    之前写过这样一个model: class MonthlyFeeMember(models.Model): worker = models.ForeignKey(Student, verbose_nam ...

  2. qt中添加Q_OBJECT报错的问题

    在qt编写的过程中添加Q_OBJECT后发现老是报错的问题 编译后老是报undefined reference to vtable for "xxx"的错误,后来发现在xxx.pr ...

  3. django安装xadmin中出现的报错汇总

    报错一:ModuleNotFoundError: No module named 'django.core.urlresolvers' ModuleNotFoundError: No module n ...

  4. Python 在已创建的数据表添加字段报错问题

    django.db.utils.IntegrityError: (1062, “Duplicate entry ’1234567891011’ for key_’dingdanid’”) 这个错误是之 ...

  5. 在web.xml中添加servlet报错问题

    出现这种问题的原因是因为servlet-name标签中没有名称,如果错误出现在servlet上,也是一样,补充servlet-name名称即可.如下图

  6. Django model 中的字段解释

    Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField:一个自动递增的整型字段,添加记录时它会自动增长.你通常不需 ...

  7. ORACLE数据导入导出后新数据库中某些表添加操作报错[ORA-12899]

    由于项目需要,我在搭建了新的开发环境后,需要将之前环境中的ORACLE数据库导出,再导入到新的开发环境下.当导出导入完成后,使用数据库进行添加操作时 发现针对很多表的添加操作报错,具体报错原因描述为: ...

  8. Django如何从Model中获取字段名称——verbose_name

    一.背景 CRM项目重新总结一下,重写之前的项目发现有不少知识点已经忘记,所以特此来重新总结一下一便后续能够回忆起 二.代码分析 1. 核心代码 from django.conf.urls impor ...

  9. Django 向数据表中添加字段方法

    在模型order中添加字段discount字段,并给予初始值0 方法: 先在models.py中修改模型 添加 discount = models.DecimalField(max_digits=8, ...

随机推荐

  1. [leetcode]301. Remove Invalid Parentheses 去除无效括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  2. golang语言基础(一)

    0.项目的组织结构: GOPATH路径下是src文件夹---->src文件夹下是项目名称---->项目名称下是该项目的各个包名---->各包名下是.go文件(一个包下,无论有多少个. ...

  3. Intellij Idea notes

    1. 解决intellij idea国际化配置文件resource bundle中文乱码问题 https://blog.csdn.net/u012453843/article/details/7531 ...

  4. ForwardPriceSimulationService

    using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Coll ...

  5. UILabel 字体下方加下划线

        UILabel *knowKankan = [[UILabel alloc] initWithFrame:CGRectMake(0, MAINHEIGHT - 78 , MAINWIDTH, ...

  6. (字典树)Revenge of Fibonacci -- HDU -- 4099

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4099 要用c++交哦, G++ MLE 不是很懂,先粘上慢慢学习 代码: #include<std ...

  7. NLog日志框架简写用法

    本文转载:http://www.blogjava.net/qiyadeng/archive/2013/02/27/395799.html 在.net中也有非常多的日志工具,今天介绍下NLog.NLog ...

  8. Codeforces768B Code For 1 2017-02-21 22:17 95人阅读 评论(0) 收藏

    B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. [Postgres]postgresql.conf : Permission denied处理一法

    使用yum安装完postgresql,没有使用默认的DATA地址,自己配置了DATA地址以后,使用root权限启动service service postgresql start ,报出了" ...

  10. 开源WebGIS实施方案(四):GeoServer发布PostGIS数据

    GeoServer可以支持多种格式的数据源,本文只介绍其中一种,即PostGIS数据源. 新建一个工作区.工作区这个名字也是一变再变,早前叫做目录,后来改为工作空间,如今已变为工作区了. 添加数据存储 ...