Django: It is impossible to add a non-nullable fieldDjango: It is impossible to add a non-nullable fieldDjango: It is impossible to add a non-nullable fieldDjango: It is impossible to add a non-nullable field
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

Django: It is impossible to add a non-nullable field

发表 admin at 2022年8月26日
类别
  • Django
标签

1. 概述:

在已有数据的表中新增字段时,会弹出下面的步骤让你来操作

Tracking file by folder pattern: migrations
It is impossible to add a non-nullable field 'Publisher' to book without specifying a default. This is because 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 manually define a default value in models.py.   #这是退出后自己输入
Select an option:

4. 解决办法

有两种:

解决方法1:

在输入框中选择    1  会弹出下面的提示

Please enter the default value as valid Python.
The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.
Type 'exit' to exit this prompt

然后输入默认的值:"1"   这里的值要记得加引号

解决方法2:

在models.py文件中 将新增的字段添加默认值

例子:

addr =models.CharField(max_length,default="默认值")

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

类别

  • Cat
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader