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实现排序算法
Feb 14 Python
python opencv设置摄像头分辨率以及各个参数的方法
Apr 02 Python
python 对dataframe下面的值进行大规模赋值方法
Jun 09 Python
python 列表转为字典的两个小方法(小结)
Jun 28 Python
TensorFlow基于MNIST数据集实现车牌识别(初步演示版)
Aug 05 Python
python时间日期操作方法实例小结
Feb 06 Python
django orm模块中的 is_delete用法
May 20 Python
Python实现爬取并分析电商评论
Jun 19 Python
详解python中的闭包
Sep 07 Python
Pandas中两个dataframe的交集和差集的示例代码
Dec 13 Python
Python趣味挑战之实现简易版音乐播放器
May 28 Python
如何利用pygame实现打飞机小游戏
May 30 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生成文件
2007/01/15 PHP
从手册去理解分析PHP session机制
2011/07/17 PHP
关于crontab的使用详解
2013/06/24 PHP
php增删改查示例自己写的demo
2013/09/04 PHP
php随机获取金山词霸每日一句的方法
2015/07/09 PHP
thinkphp5框架API token身份验证功能示例
2019/05/21 PHP
Swoole扩展的6种模式深入详解
2021/03/04 PHP
jquery select下拉框操作的一些说明
2010/04/02 Javascript
过虑特殊字符输入的js代码
2010/08/05 Javascript
在多个页面使用同一个HTML片段《续》
2011/03/04 Javascript
javascript数字格式化通用类 accounting.js使用
2012/08/24 Javascript
解决火狐浏览器下JS setTimeout函数不兼容失效不执行的方法
2012/11/14 Javascript
jquery实现表格奇数偶数行不同样式(有图为证及实现代码)
2013/01/23 Javascript
jQuery动态修改超链接地址的方法
2015/02/13 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
js中string和number类型互转换技巧(分享)
2016/11/28 Javascript
NodeJS基础API搭建服务器详细过程记录
2017/04/01 NodeJs
vue 实现通过手机发送短信验证码注册功能
2018/04/19 Javascript
使用VScode 插件debugger for chrome 调试react源码的方法
2019/09/13 Javascript
[02:54]辉夜杯主赛事第二日败者组 iG.V赛后采访
2015/12/26 DOTA
Python元字符的用法实例解析
2018/01/17 Python
zookeeper python接口实例详解
2018/01/18 Python
解决python3中cv2读取中文路径的问题
2018/12/05 Python
对Python Pexpect 模块的使用说明详解
2019/02/14 Python
快速排序的四种python实现(推荐)
2019/04/03 Python
基于Python实现扑克牌面试题
2019/12/11 Python
Python中操作各种多媒体,视频、音频到图片的代码详解
2020/06/04 Python
奥地利手表、香水、化妆品和珠宝购物网站:Brasty.at
2021/01/17 全球购物
员工工作表扬信范文
2014/01/13 职场文书
汽车队司机先进事迹材料
2014/02/01 职场文书
学校关爱留守儿童活动方案
2014/08/27 职场文书
效能风暴心得体会
2014/09/04 职场文书
篮球友谊赛通讯稿
2014/10/10 职场文书
幼儿园大班教师个人总结
2015/02/05 职场文书
幼儿园门卫安全责任书
2015/05/08 职场文书
Ajax请求超时与网络异常处理图文详解
2021/05/23 Javascript