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 2.7.x 和 3.x 版本的重要区别小结
Nov 28 Python
Python网络爬虫项目:内容提取器的定义
Oct 25 Python
python3中set(集合)的语法总结分享
Mar 24 Python
python中使用PIL制作并验证图片验证码
Mar 15 Python
解读python logging模块的使用方法
Apr 17 Python
对dataframe数据之间求补集的实例详解
Jan 30 Python
python 随机森林算法及其优化详解
Jul 11 Python
如何基于Python批量下载音乐
Nov 11 Python
python实现图像拼接
Mar 05 Python
python 获取当前目录下的文件目录和文件名实例代码详解
Mar 10 Python
基于python代码批量处理图片resize
Jun 04 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实现的美国50个州选择列表实例
2015/04/20 PHP
PHP 闭包详解及实例代码
2016/09/28 PHP
dojo 之基础篇
2007/03/24 Javascript
如何用ajax来创建一个XMLHttpRequest对象
2012/12/10 Javascript
js变量、作用域及内存详解
2014/09/23 Javascript
JS实现不使用图片仿Windows右键菜单效果代码
2015/10/22 Javascript
JavaScript中split与join函数的进阶使用技巧
2016/05/03 Javascript
jQuery利用sort对DOM元素进行排序操作
2016/11/07 Javascript
使用 jQuery 实现表单验证功能
2017/07/05 jQuery
详解webpack多页面配置记录
2018/01/22 Javascript
JavaScript实现汉字转换为拼音及缩写的方法示例
2019/03/28 Javascript
vue登录以及权限验证相关的实现
2019/10/25 Javascript
使用vue实现HTML页面生成图片的方法
2020/03/12 Javascript
[02:16]DOTA2超级联赛专访Burning 逆袭需要抓住机会
2013/06/24 DOTA
Python中的with语句与上下文管理器学习总结
2016/06/28 Python
Python实现mysql数据库更新表数据接口的功能
2017/11/19 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
2018/01/08 Python
对Tensorflow中权值和feature map的可视化详解
2018/06/14 Python
在Pycharm terminal中字体大小设置的方法
2019/01/16 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
Django中Aggregation聚合的基本使用方法
2020/07/09 Python
如何利用Python写个坦克大战
2020/11/18 Python
使用django自带的user做外键的方法
2020/11/30 Python
解决HTML5中的audio在手机端和微信端的不能自动播放问题
2019/11/04 HTML / CSS
美国羊皮公司:Overland
2018/01/15 全球购物
描述RIP和OSPF区别以及特点
2015/01/17 面试题
人力资源部培训专员岗位职责
2014/01/02 职场文书
青年创业培训欢迎词
2014/01/08 职场文书
焦点访谈观后感
2015/06/11 职场文书
初级职称评定工作总结
2015/08/13 职场文书
2016年教师新年寄语
2015/08/18 职场文书
中考百日冲刺决心书
2015/09/22 职场文书
大学生村官驻村工作心得体会
2016/01/23 职场文书
导游词之西安骊山
2019/12/20 职场文书
基于MySql验证的vsftpd虚拟用户
2021/11/07 MySQL
关于vue-router-link选择样式设置
2022/04/30 Vue.js