# 在Model中
class Meta:
db_table = "new_tablename" # 更改表名
1.直接在models.py中增加
class User(models.Model):
username=models.CharField(max_length=50)
password=models.CharField(max_length=50)
zhname = models.CharField(max_length=50)
我增加了zhname = models.CharField(max_length=50)
再依次執行
1.python manage.py makemigrations
2.python manage.py migrate
執行第一句時報錯
You are trying to add a non-nullable field 'name' to contact without a default; 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 with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
解決方法:給zhname 給初值’abc’
zhname = models.CharField(max_length=50,default='abc')
然後依次執行
python manage.py makemigrations
python manage.py migrate
刪除default默認初始值default=‘abc’
再執行
python manage.py makemigrations
python manage.py migrate