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中日期和时间格式化输出的方法小结
Mar 19 Python
Python过滤列表用法实例分析
Apr 29 Python
Python+Selenium自动化实现分页(pagination)处理
Mar 31 Python
python通过伪装头部数据抵抗反爬虫的实例
May 07 Python
Pycharm新建模板默认添加个人信息的实例
Jul 15 Python
python类中super() 的使用解析
Dec 19 Python
Python 统计位数为偶数的数字代码详解
Mar 15 Python
TensorFlow2.X结合OpenCV 实现手势识别功能
Apr 08 Python
在keras下实现多个模型的融合方式
May 23 Python
python中id函数运行方式
Jul 03 Python
python实现股票历史数据可视化分析案例
Jun 10 Python
python基础入门之普通操作与函数(三)
Jun 13 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
Excel数据导入Mysql数据库的实现代码
2008/06/05 PHP
php检测图片木马多进制编程实践
2013/04/11 PHP
php+js实现图片的上传、裁剪、预览、提交示例
2013/08/27 PHP
PHP图片等比缩放类SimpleImage使用方法和使用实例分享
2014/04/10 PHP
PHP中魔术变量__METHOD__与__FUNCTION__的区别
2014/09/29 PHP
php实现遍历多维数组的方法
2015/11/25 PHP
PHP简单创建压缩图的方法
2016/08/24 PHP
PHP数组遍历的几种常见方式总结
2019/02/15 PHP
jquery实现点击消失的代码
2014/03/03 Javascript
jQuery响应鼠标事件并隐藏与显示input默认值
2014/08/24 Javascript
js实现文本框中输入文字页面中div层同步获取文本框内容的方法
2015/03/03 Javascript
用svg制作富有动态的tooltip
2015/07/17 Javascript
JS实现单击输入框弹出选择框效果完整实例
2015/12/14 Javascript
AngularJS  自定义指令详解及实例代码
2016/09/14 Javascript
Vue.js Ajax动态参数与列表显示实现方法
2016/10/20 Javascript
原生js实现弹出层效果
2017/01/20 Javascript
详解AngularJS1.6版本中ui-router路由中/#!/的解决方法
2017/05/22 Javascript
Angular4学习笔记之准备和环境搭建项目
2017/08/01 Javascript
vue数据控制视图源码解析
2018/03/28 Javascript
运用js实现图层拖拽的功能
2019/05/24 Javascript
node.js的http.createServer过程深入解析
2019/06/06 Javascript
jQuery+ajax实现文件上传功能
2020/12/22 jQuery
Python中使用platform模块获取系统信息的用法教程
2016/07/08 Python
win与linux系统中python requests 安装
2016/12/04 Python
python将ansible配置转为json格式实例代码
2017/05/15 Python
Python实现树的先序、中序、后序排序算法示例
2017/06/23 Python
python中列表和元组的区别
2017/12/18 Python
Python 删除整个文本中的空格,并实现按行显示
2018/07/24 Python
通过celery异步处理一个查询任务的完整代码
2019/11/19 Python
基于selenium及python实现下拉选项定位select
2020/07/22 Python
Python利用Faiss库实现ANN近邻搜索的方法详解
2020/08/03 Python
国际礼品店:GiftsnIdeas
2018/05/03 全球购物
企划主管岗位职责
2013/12/12 职场文书
企业项目策划书
2014/01/11 职场文书
2015年庆祝国庆节66周年演讲稿
2015/07/30 职场文书
筑梦中国心得体会
2016/01/18 职场文书