django 在原有表格添加或删除字段的实例
作者:JackLiu16 发布时间:2023-11-25 04:21:08
标签:django,添加,删除,字段
一、如果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>
来源:https://blog.csdn.net/JackLiu16/article/details/78819194


猜你喜欢
- 引言本篇文章记录仿写一个el-collapse组件细节,从而有助于大家更好理解饿了么ui对应组件具体工作细节。本文是elementui源码学
- 问题描述尝试用Python写一个Wordcloud的时候,出现了编码问题。照着网上某些博客的说法添添改改后,结果是变成了“UnicodeDe
- Vuex使用单一状态树(一个对象就包含了全部的应用层级状态),它作为唯一数据源存在,每个应用仅仅有一个store实例。单一状态树使得我们能够
- 最近自己做的项目中设计师要求分类栏中鼠标悬停更换图片,大致实现出来的效果就是这样: 这个在jQuery中是个很简单的事,但是在vue中我还是
- 在使用ORACLE的过程过,我们会经常遇到一些ORACLE产生的错误,对于初学者而言,这些错误可能有点模糊,而且可能一时不知怎么去处理产生的
- 上一篇文章中我们介绍了 python 语言的几个特点,并在最后留了一个问题,python 除了上下执行以外有没有其他的执行方式。今天我们就来
- Saver的用法1. Saver的背景介绍我们经常在训练完一个模型之后希望保存训练的结果,这些结果指的是模型的参数,以便下次迭代的训练或者用
- Paramiko是一个用于执行SSH命令的Python第三方库,使用该库可实现自动化运维的所有任务,如下是一些常用代码的封装方式,多数代码为
- 结果然后直接放源码:import cv2 as cvsource = cv.imread("zhaopian.jpg")
- python中有一个略微奇怪的表达式叫yield expression,本文就来探究一下这是个什么东西。一步一步来。iterablemyli
- 我是从去年初开始学习web标准的,两年下来也有些心得。最近跳槽了正好闲在家里,写一些出来和大家交流一下。1对于web标准和W3C XHTML
- 本文是在上篇文章Python中的type和object,做的补充,希望大家喜欢。这篇博客主要描述Python的新风格对象(new-style
- Oracle 的正规表达式的实施是以各种 SQL 函数和一个 WHERE 子句操作符的形式出现的。如果您不熟悉正规表达式,那么这篇文章可以让
- Python 环境安装 下载 Python 安装包进入 python 官网 ,在Downloads(下载)下面,点击 Window 进入下载
- 本文实例讲述了php生成curl命令行的方法。分享给大家供大家参考,具体如下:示例:curl "http://localhost/
- Python sorted() 函数sorted() 函数对所有可迭代的对象进行排序操作sorted 语法:sorted(iterable,
- fnamtch就是filenamematch, 在python中利用符合linuxshell风格的匹配模块来进行文件名的匹配筛选工作。fnm
- 本文实例讲述了php获取客户端IP及URL的方法。分享给大家供大家参考,具体如下:function getonlineip(){//获取用户
- 一般的多数据库支持在配置文件中如下: <connectionStrings> <add name="MyCmsC
- 下面提供生成XML的Google SiteMap代码[ASP版本]。这个代码是生成全站文件链接的地图:<%Server.S