激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - django 在原有表格添加或刪除字段的實(shí)例

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

2021-02-25 00:53JackLiu16 Python

今天小編就為大家分享一篇django 在原有表格添加或刪除字段的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

一、如果models.py文件為時(shí):

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

會(huì)提示:

?
1
2
3
4
5
6
(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

?
1
2
3
4
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()
>>>

這里面不好修改

可以

?
1
2
3
4
5
(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í):

?
1
2
timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=False, null=False)
timestamp = models.DateTimeField('保存日期',default=timezone.now,blank=True, null=True)

blank

設(shè)置為True時(shí),字段可以為空。設(shè)置為False時(shí),字段是必須填寫的。字符型字段CharField和TextField是用空字符串來(lái)存儲(chǔ)空值的。如果為True,字段允許為空,默認(rèn)不允許.

null

設(shè)置為True時(shí),django用Null來(lái)存儲(chǔ)空值。日期型、時(shí)間型和數(shù)字型字段不接受空字符串。所以設(shè)置IntegerField,DateTimeField型字段可以為空時(shí),需要將blank,null均設(shè)為True。如果為True,空值將會(huì)被存儲(chǔ)為NULL,默認(rèn)為False。如果想設(shè)置BooleanField為空時(shí)可以選用NullBooleanField型字段。

?
1
2
(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)站開發(fā)的核心

補(bǔ)充:timestamp = models.DateTimeField('保存日期')

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(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è)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/JackLiu16/article/details/78819194

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本在线一区二区 | 一区二区三区国产视频 | 中文字幕一区二区三区久久 | 一区二区三区欧美日韩 | 国产另类一区 | 一级片a | 欧美日韩在线免费观看 | 男人午夜小视频 | www69xxxxx| 史上最强炼体老祖动漫在线观看 | 日日操日日操 | 午夜生活理论片 | 亚洲视频黄 | 国产欧美日韩视频在线观看 | 黄色av免费网站 | 黄色av免费电影 | 国产美女视频一区二区三区 | 中午日产幕无线码1区 | 国产精品久久久久一区二区 | 91不雅视频| 国产精品久久久久久久久久东京 | 91短视频网址 | 精品一区二区三区免费 | 黄色的视频在线观看 | 欧美a区 | 一二区电影 | 久久久久久久久淑女av国产精品 | 成人在线第一页 | 伦一区二区三区中文字幕v亚洲 | 二区视频 | 49vv看片免费 | 亚洲乱操 | 亚洲伊人色欲综合网 | 99成人在线 | 欧美成人激情在线 | 日日做夜夜操 | 天天色综合6 | 黄色一级片在线免费观看 | 日韩 欧美 中文 | 女人一级一级毛片 | 国产在线精品一区二区三区不卡 |