国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

django 在原有表格添加或刪除字段的實例

2020-02-15 21:31:48
字體:
來源:轉載
供稿:網友

一、如果models.py文件為時:

timestamp = models.DateTimeField('保存日期')

會提示:

(env8) D:/Desktop/env8/Scripts/mysite>python manage.py makemigrationsYou are trying to add a non-nullable field 'timestamp' to article without a default; 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 PythonThe 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 makemigrationsYou are trying to add a non-nullable field 'timestamp' to article without a default; 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.pySelect an option: 1Please enter the default value now, as valid PythonThe 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 migrateOperations to perform: Synchronize unmigrated apps: staticfiles, ckeditor_uploader, messages, ckeditor, bootstrap3 Apply all migrations: admin, blog, contenttypes, auth, sessionsSynchronizing 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-packages/django/db/models/fields/__init__.py:1474: RuntimeWarning: DateTimeField Article.timestamp received a naive datetime (2017-12-16 05:04:31) while time zone support is active. RuntimeWarning) OK(env8) D:/Desktop/env8/Scripts/mysite>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定远县| 澄迈县| 丹棱县| 定结县| 孙吴县| 黔南| 禹州市| 容城县| 闵行区| 九龙县| 定日县| 伊川县| 恩平市| 江阴市| 巧家县| 余江县| 新营市| 江津市| 措美县| 丹江口市| 金川县| 时尚| 莱芜市| 锦屏县| 隆德县| 平顺县| 阳原县| 乌鲁木齐县| 玛多县| 沙河市| 敦煌市| 米林县| 承德市| 溧阳市| 深水埗区| 比如县| 漯河市| 简阳市| 乐东| 阿拉善左旗| 凯里市|