在你改動了 model.py的內容之後執行下面的命令:
python manage.py makemigrations
Migrations for 'users':
mall/apps/users/migrations/0001_initial.py
- Create model User
相當於 在該app下建立 migrations目錄,並記錄下你所有的關於modes.py的改動,
比如0001_initial.py, 但是這個改動還沒有作用到數據庫文件
你可以手動打開這個文件,看看裏面是什麼
根據檢測到的模型創建新的遷移。遷移的作用,更多的是將數據庫的操作,以文件的形式記錄下來,方便以後檢查、調用、重做等等。尤其是對於Git版本管理,它無法獲知數據庫是如何變化的,只能通過遷移文件中的記錄來追溯和保存。
在此之後執行命令
python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, users
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying users.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying sessions.0001_initial... OK
使數據庫狀態與當前模型集和遷移集同步。其實就是將對數據庫的更改,主要是數據表設計的更改,在數據庫中真實執行。例如,新建、修改、刪除數據表,新增、修改、刪除某數據表內的字段等等。