欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年05月27日 15:50:25   作者:JackLiu16  
今天小編就為大家分享一篇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來存儲空值。日期型、時間型和數(shù)字型字段不接受空字符串。所以設置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 就行了中間不會設置數(shù)據(jù)類型(很容易出錯)(若要設置默認值)

三、數(shù)據(jù)庫設計是整個網(wǎng)站開發(fā)的核心

補充: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 在原有表格添加或刪除字段的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • python 數(shù)據(jù)分析實現(xiàn)長寬格式的轉(zhuǎn)換

    python 數(shù)據(jù)分析實現(xiàn)長寬格式的轉(zhuǎn)換

    這篇文章主要介紹了python 數(shù)據(jù)分析實現(xiàn)長寬格式的轉(zhuǎn)換,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 用Python爬取2022春節(jié)檔電影信息

    用Python爬取2022春節(jié)檔電影信息

    大家好,本篇文章主要講的是用Python爬取2022春節(jié)檔電影信息,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • python殺死一個線程的方法

    python殺死一個線程的方法

    由于python線程沒有提供abort方法,所以我們需要自己想辦法解決此問題,面對這一問題,小編幫大家解決phthon殺死一個線程的方法,需要的朋友一起來學習吧
    2015-09-09
  • 如何搜索查找并解決Django相關的問題

    如何搜索查找并解決Django相關的問題

    每個程序員都會在開發(fā)過程中遇到這樣或那樣的問題, 有時光靠一個人是無法解決所有問題的, 所以我們應該找到適當?shù)牡胤教釂?
    2014-06-06
  • Python讀取ini配置文件傳參的簡單示例

    Python讀取ini配置文件傳參的簡單示例

    這篇文章主要給大家介紹了關于Python讀取ini配置文件傳參的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 在Python中使用swapCase()方法轉(zhuǎn)換大小寫的教程

    在Python中使用swapCase()方法轉(zhuǎn)換大小寫的教程

    這篇文章主要介紹了在Python中使用swapCase()方法轉(zhuǎn)換大小寫的教程,是Python入門中的基礎知識,需要的朋友可以參考下
    2015-05-05
  • python的setattr函數(shù)實例用法

    python的setattr函數(shù)實例用法

    在本篇文章里小編給大家整理了一篇關于python的setattr函數(shù)實例用法的相關知識點內(nèi)容,有興趣的朋友們學習下。
    2020-12-12
  • Matplotlib實戰(zhàn)之直方圖繪制詳解

    Matplotlib實戰(zhàn)之直方圖繪制詳解

    直方圖,又稱質(zhì)量分布圖,用于表示數(shù)據(jù)的分布情況,是一種常見的統(tǒng)計圖表,這篇文章主要為大家詳細介紹了如何使用Matplotlib繪制直方圖,需要的可以參考下
    2023-08-08
  • python去除文件中重復的行實例

    python去除文件中重復的行實例

    今天小編就為大家分享一篇python去除文件中重復的行實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python設計模式之抽象工廠模式

    Python設計模式之抽象工廠模式

    這篇文章主要為大家詳細介紹了Python設計模式之抽象工廠模式,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論