django 在原有表格添加或删除字段的实例


Posted in Python onMay 27, 2018

一、如果models.py文件为时:

timestamp = models.DateTimeField('保存日期')

会提示:

(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations
You are trying to add a non-nullable field 'timestamp' to article without a defa
ult; 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)
 2) Quit, and let me add a default in models.py

输入:1 (这里要求你设置新建字段的默认值,它会在新建这个字段的同时把默认值也添加上去,)Select an option: 1

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g.
 timezone.now()
>>>

这里面不好修改

可以

(env8) D:\Desktop\env8\Scripts\mysite>python manage.py shell
(env8) D:\Desktop\env8\Scripts\mysite>from django.db import connection
(env8) D:\Desktop\env8\Scripts\mysite>cursor=connection.cursor()
(env8) D:\Desktop\env8\Scripts\mysite>cursor.execute('ALTER TABLEArticle add column timestamp varchar(100) default 0')

二、如果models.py文件为时:

timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=False, null=False)
timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=True, null=True)

blank

设置为True时,字段可以为空。设置为False时,字段是必须填写的。字符型字段CharField和TextField是用空字符串来存储空值的。如果为True,字段允许为空,默认不允许.

null

设置为True时,django用Null来存储空值。日期型、时间型和数字型字段不接受空字符串。所以设置IntegerField,DateTimeField型字段可以为空时,需要将blank,null均设为True。如果为True,空值将会被存储为NULL,默认为False。如果想设置BooleanField为空时可以选用NullBooleanField型字段。

(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations就不会有下面的提示
(env8) D:\Desktop\env8\Scripts\mysite>python manage.py migrate 就行了中间不会设置数据类型(很容易出错)(若要设置默认值)

三、数据库设计是整个网站开发的核心

补充:timestamp = models.DateTimeField('保存日期')

(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations
You are trying to add a non-nullable field 'timestamp' to article without a defa
ult; 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)
 2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g.
 timezone.now()
>>> '2017-12-16 05:04:31.000'(添加字段的数据类型格式)
Migrations for 'blog':
 0002_article_timestamp.py:
  - Add field timestamp to article
(env8) D:\Desktop\env8\Scripts\mysite>python manage.py migrate
Operations to perform:
 Synchronize unmigrated apps: staticfiles, ckeditor_uploader, messages, ckedito
r, bootstrap3
 Apply all migrations: admin, blog, contenttypes, auth, sessions
Synchronizing apps without migrations:
 Creating tables...
  Running deferred SQL...
 Installing custom SQL...
Running migrations:
 Rendering model states... DONE
 Applying blog.0002_article_timestamp...D:Desktop\env8\lib\site-packa
ges\django\db\models\fields\__init__.py:1474: RuntimeWarning: DateTimeField Arti
cle.timestamp received a naive datetime (2017-12-16 05:04:31) while time zone su
pport is active.
 RuntimeWarning)
 OK
(env8) D:\Desktop\env8\Scripts\mysite>

以上这篇django 在原有表格添加或删除字段的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python交换变量
Sep 06 Python
Python 时间处理datetime实例
Sep 06 Python
一个简单的python程序实例(通讯录)
Nov 29 Python
python实现日常记账本小程序
Mar 10 Python
python实现简单登陆系统
Oct 18 Python
使用11行Python代码盗取了室友的U盘内容
Oct 23 Python
Python中按键来获取指定的值
Mar 02 Python
python实现植物大战僵尸游戏实例代码
Jun 10 Python
python Event事件、进程池与线程池、协程解析
Oct 25 Python
python 实现Flask中返回图片流给前端展示
Jan 09 Python
Python3 Click模块的使用方法详解
Feb 12 Python
Selenium环境变量配置(火狐浏览器)及验证实现
Dec 07 Python
用python写扫雷游戏实例代码分享
May 27 #Python
和孩子一起学习python之变量命名规则
May 27 #Python
儿童学习python的一些小技巧
May 27 #Python
django初始化数据库的实例
May 27 #Python
django 删除数据库表后重新同步的方法
May 27 #Python
Django 根据数据模型models创建数据表的实例
May 27 #Python
Django使用Mysql数据库已经存在的数据表方法
May 27 #Python
You might like
PHP操作数组相关函数
2011/02/03 PHP
php实现留言板功能(代码详解)
2017/03/28 PHP
PHP设计模式之原型模式定义与用法详解
2018/04/03 PHP
Thinkphp整合阿里云OSS图片上传实例代码
2019/04/28 PHP
php实现的顺序线性表示例
2019/05/04 PHP
二级域名转向类
2006/11/09 Javascript
JQuery防止退格键网页后退的实现代码
2012/03/23 Javascript
基于jquery的不规则矩形的排列实现代码
2012/04/16 Javascript
js关闭子窗体刷新父窗体实现方法
2012/12/04 Javascript
JavaScrip实现PHP print_r的数功能(三种方法)
2013/11/12 Javascript
用原生js做个简单的滑动效果的回到顶部
2014/10/15 Javascript
nodejs实现的一个简单聊天室功能分享
2014/12/06 NodeJs
jQuery中before()方法用法实例
2014/12/25 Javascript
原生js实现class的添加和删除简单代码
2016/07/12 Javascript
浅谈angularjs module返回对象的坑(推荐)
2016/10/21 Javascript
jquery 删除节点 添加节点 找兄弟节点的简单实现
2016/12/07 Javascript
Bootstrap实现圆角、圆形头像和响应式图片
2016/12/14 Javascript
BootStrap框架中的data-[ ]自定义属性理解(推荐)
2017/02/14 Javascript
jQuery EasyUI Draggable拖动组件
2017/03/01 Javascript
前端构建工具之gulp的配置与搭建详解
2017/06/12 Javascript
详解Vue 2.0封装axios笔记
2017/06/22 Javascript
jQuery实现的点击显示隐藏下拉菜单功能完整示例
2019/05/17 jQuery
vue实现随机验证码功能(完整代码)
2019/12/10 Javascript
使用Python脚本来控制Windows Azure的简单教程
2015/04/16 Python
python检查序列seq是否含有aset中项的方法
2015/06/30 Python
python数组过滤实现方法
2015/07/27 Python
Python迭代和迭代器详解
2016/11/10 Python
一文带你了解Python中的字符串是什么
2018/11/20 Python
Python 根据日志级别打印不同颜色的日志的方法示例
2019/08/08 Python
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
2019/09/23 Python
html5 datalist 选中option选项后的触发事件
2020/03/05 HTML / CSS
英国DIY汽车维修配件网站:DIY Car Service Parts
2019/08/30 全球购物
妇产科护士自我鉴定
2013/10/15 职场文书
杨善洲电影观后感
2015/06/04 职场文书
python利用pandas分析学生期末成绩实例代码
2021/07/09 Python
Win11无法访问设备和打印机 如何解决页面空白
2022/04/09 数码科技