一、model文件添加字段
可根據(jù)字段要求設(shè)置屬性辅柴,如字段類型箩溃、是否為null
,默認(rèn)值等
from django.db import models
# Create your models here.
class Case(models.Model):
run_time = models.CharField(max_length=100, default='2022-05-20 13:43:38') # 運(yùn)行時(shí)間點(diǎn)
def __str__(self):
return str(self.id)
二碌识、遷移數(shù)據(jù)
1碾篡、項(xiàng)目文件下執(zhí)行命令:
#添加遷移事務(wù)
python manage.py makemigrations
#將遷移標(biāo)記為以應(yīng)用
python manage.py migrate
$ python manage.py makemigrations
Migrations for 'App':
App/migrations/0019_auto_20220520_1510.py
- Alter field run_time on case
$ python manage.py migrate
Operations to perform:
Apply all migrations: App, admin, auth, authtoken, contenttypes, django_cas_ng, sessions
Running migrations:
Applying App.0019_auto_20220520_1510... OK
2虱而、遷移完成后筏餐,將生成遷移文件
3、遷移完成后牡拇,新字段添加成功
三魁瞪、撤銷遷移
1、撤銷上一次遷移數(shù)據(jù)
可以通過(guò) migrate
傳遞上一次遷移的編號(hào)來(lái)撤銷遷移惠呼。
例如导俘,要撤銷最近一次遷移 0020_auto_20220520_1511
,進(jìn)入遷移文件剔蹋,找到dependencies
中信息
dependencies = [
('App', '0019_auto_20220520_1510'),
]
命令行中執(zhí)行撤銷:
python manage.py migrate App 0019
$ python manage.py migrate App 0019
Operations to perform:
Target specific migration: 0019_auto_20220520_1510, from App
Running migrations:
Rendering model states... DONE
Unapplying App.0020_auto_20220520_1511... OK
2旅薄、撤銷應(yīng)應(yīng)用于一個(gè)應(yīng)用的所有遷移
python manage.py migrate App zero