香港公厕门事件,gts除皱,bzmh
一、如果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 在原有表格添加或删除字段的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论