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中的高级函数map/reduce使用实例
Apr 13 Python
python字典多键值及重复键值的使用方法(详解)
Oct 31 Python
详解python进行mp3格式判断
Dec 23 Python
利用python实现数据分析
Jan 11 Python
Python缓存技术实现过程详解
Sep 25 Python
python实现俄罗斯方块游戏(改进版)
Mar 13 Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
Apr 23 Python
使用 django orm 写 exists 条件过滤实例
May 20 Python
Numpy中的数组搜索中np.where方法详细介绍
Jan 08 Python
pandas 操作 Excel操作总结
Mar 31 Python
k-means & DBSCAN 总结
Apr 27 Python
Python matplotlib可视化之绘制韦恩图
Feb 24 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 5.3.x 的strtotime() 时区设定 警告信息修复
2013/08/05 PHP
php创建sprite
2014/02/11 PHP
Laravel框架数据库CURD操作、连贯操作总结
2014/09/03 PHP
PHP CURL使用详解
2019/03/21 PHP
js css样式操作代码(批量操作)
2009/10/09 Javascript
flexigrid 类似ext grid的JS表格代码
2010/07/17 Javascript
基于JavaScript 数据类型之Boolean类型分析介绍
2013/04/19 Javascript
JQuery 在线引用及测试引用是否成功
2014/06/24 Javascript
node.js cookie-parser 中间件介绍
2016/06/06 Javascript
Bootstrap按钮组简单实现代码
2017/03/06 Javascript
不到200行 JavaScript 代码实现富文本编辑器的方法
2018/01/03 Javascript
vue axios登录请求拦截器
2018/04/02 Javascript
Vue.js点击切换按钮改变内容的实例讲解
2018/08/22 Javascript
微信小程序使用wxParse解析html的方法示例
2019/01/17 Javascript
详解js 创建对象的几种方法
2019/03/08 Javascript
[01:07:19]2018DOTA2亚洲邀请赛 4.5 淘汰赛 Mineski vs VG 第一场
2018/04/06 DOTA
Python 查找字符在字符串中的位置实例
2018/05/02 Python
详解python3 + Scrapy爬虫学习之创建项目
2019/04/12 Python
快速解决pyqt5窗体关闭后子线程不同时退出的问题
2019/06/19 Python
flask应用部署到服务器的方法
2019/07/12 Python
Pytorch中Tensor与各种图像格式的相互转化详解
2019/12/26 Python
opencv python Canny边缘提取实现过程解析
2020/02/03 Python
python3.6环境下安装freetype库和基本使用方法(推荐)
2020/05/10 Python
HTML5 Blob 实现文件下载功能的示例代码
2019/11/29 HTML / CSS
美国第二大团购网站:LivingSocial
2016/07/24 全球购物
一套软件开发工程师笔试题
2015/05/18 面试题
HR喜欢的自荐信格式
2013/10/08 职场文书
超市总经理岗位职责
2014/02/02 职场文书
读书伴我成长演讲稿
2014/05/07 职场文书
离婚协议书的书写要求
2014/09/17 职场文书
胡雪岩故居导游词
2015/02/06 职场文书
工程合作意向书范本
2015/05/09 职场文书
python - timeit 时间模块
2021/04/06 Python
MySQL子查询中order by不生效问题的解决方法
2021/08/02 MySQL
Tomcat执行startup.bat出现闪退的原因及解决办法
2022/04/20 Servers
Win11任务栏无法正常显示 资源管理器不停重启的解决方法
2022/07/07 数码科技