django 在原有表格添加或刪除字段的實(shí)例
一、如果models.py文件為時(shí):
timestamp = models.DateTimeField('保存日期')
會(huì)提示:
(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 (這里要求你設(shè)置新建字段的默認(rèn)值,它會(huì)在新建這個(gè)字段的同時(shí)把默認(rèn)值也添加上去,)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文件為時(shí):
timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=False, null=False)
timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=True, null=True)
blank
設(shè)置為T(mén)rue時(shí),字段可以為空。設(shè)置為False時(shí),字段是必須填寫(xiě)的。字符型字段CharField和TextField是用空字符串來(lái)存儲(chǔ)空值的。如果為T(mén)rue,字段允許為空,默認(rèn)不允許.
null
設(shè)置為T(mén)rue時(shí),django用Null來(lái)存儲(chǔ)空值。日期型、時(shí)間型和數(shù)字型字段不接受空字符串。所以設(shè)置IntegerField,DateTimeField型字段可以為空時(shí),需要將blank,null均設(shè)為T(mén)rue。如果為T(mén)rue,空值將會(huì)被存儲(chǔ)為NULL,默認(rèn)為False。如果想設(shè)置BooleanField為空時(shí)可以選用NullBooleanField型字段。
(env8) D:\Desktop\env8\Scripts\mysite>python manage.py makemigrations就不會(huì)有下面的提示 (env8) D:\Desktop\env8\Scripts\mysite>python manage.py migrate 就行了中間不會(huì)設(shè)置數(shù)據(jù)類型(很容易出錯(cuò))(若要設(shè)置默認(rèn)值)
三、數(shù)據(jù)庫(kù)設(shè)計(jì)是整個(gè)網(wǎng)站開(kāi)發(fā)的核心
補(bǔ)充: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'(添加字段的數(shù)據(jù)類型格式) 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 在原有表格添加或刪除字段的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
書(shū)寫(xiě)Python代碼的一種更優(yōu)雅方式(推薦!)
Python是一個(gè)高層次的結(jié)合了解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語(yǔ)言,下面這篇文章主要給大家介紹了關(guān)于書(shū)寫(xiě)Python代碼的一種更優(yōu)雅方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備
這篇文章主要介紹了如何在Python3中使用telnetlib模塊連接網(wǎng)絡(luò)設(shè)備,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Python?加載?TensorFlow?模型的注意事項(xiàng)
TensorFlow支持多種模型格式,但最常見(jiàn)的兩種是SavedModel和HDF5(對(duì)于Keras模型),這里,我將分別給出加載這兩種模型格式的示例代碼,需要的朋友可以參考下2024-08-08
8種用Python實(shí)現(xiàn)線性回歸的方法對(duì)比詳解
這篇文章主要介紹了8種用Python實(shí)現(xiàn)線性回歸的方法對(duì)比詳解,說(shuō)到如何用Python執(zhí)行線性回歸,大部分人會(huì)立刻想到用sklearn的linear_model,但事實(shí)是,Python至少有8種執(zhí)行線性回歸的方法,sklearn并不是最高效的,需要的朋友可以參考下2019-07-07
Python函數(shù)必須先定義,后調(diào)用說(shuō)明(函數(shù)調(diào)用函數(shù)例外)
這篇文章主要介紹了Python函數(shù)必須先定義,后調(diào)用說(shuō)明(函數(shù)調(diào)用函數(shù)例外),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python用TensorFlow做圖像識(shí)別的實(shí)現(xiàn)
這篇文章主要介紹了python用TensorFlow做圖像識(shí)別的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
基于jupyter代碼無(wú)法在pycharm中運(yùn)行的解決方法
這篇文章主要介紹了基于jupyter代碼無(wú)法在pycharm中運(yùn)行的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

