django1 9 實現模型變更三步走

2021-07-15 09:49:04 字數 1315 閱讀 1030

tust@tust:~$ python

python 2.7.6 (default, mar 22 2014, 22:59:38)

[gcc 4.8.2] on linux2

>>> import django

>>> django.version

(1, 9, 7, 'final', 0)

執行此命令提示沒有次命令:

tust@tust:~/djcode/mysite$ python manage.py syncdb

unknown command: 'syncdb'

type 'manage.py help' for usage.

檢視django中文文件,

內容如下:

實現模型變更的三個步驟:

修改你的模型(在models.py檔案中)。

執行python manage.py makemigrations ,為這些修改建立遷移檔案

執行python manage.py migrate ,將這些改變更新到資料庫中。

1.生成漂移檔案。

tust@tust:~/djcode/mysite$ python manage.py makemigrations books

migrations for 'books':

0001_initial.py:

- create model author

- create model book

- create model publisher

- add field publisher to book

2.同步到資料庫

tust@tust:~/djcode/mysite$ python manage.py migrate

operations to perform:

running migrations:

rendering model states... done

檢視相應資料庫,發現已將模型同步到資料庫,具體資料庫內容不再贅述。

另註:

python manage.py sqlmigrate books  0001
其中,sqlmigrate命令並不會在你的資料庫上真正執行遷移檔案

—— 它只是把django 認為需要的sql列印在螢幕上以讓你能夠看到。

這對於檢查django將要進行的資料庫操作或者你的資料庫管理員需要這些sql指令碼是非常有用的。

Django1 9學習筆記

等 django1.9的templates系統和1.7有些許不一樣,原來的templates系統要由使用者自己定義。如 template path os.path.join base dir,templates template dirs put strings here,like home htm...

Django 1 9 靜態檔案配置

django 靜態檔案的配置涉及到settings 中的三個配置項static url staticfiles dirs及static root 1.開發伺服器中靜態檔案的配置及應用 1 static url 配置 預設情況下 settings 配置 static url static 2 stat...

Django 1 9 開發web專案

django.contrib.admin django.contrib.auth django.contrib.contenttypes django.contrib.sessions django.contrib.messages django.contrib.staticfiles rest f...