1. 程式人生 > >python學習2-資料庫配置操作

python學習2-資料庫配置操作

1.建立實體類models-自定義資料庫的欄位與名稱

class Register (models.Model):
idCard=models.CharField(max_length=18)
pwd=models.CharField(max_length=32,db_column='record_content')
class Meta:
db_table="new_tablename"
備註:可以指定資料庫的名稱,欄位對應的資料庫地段
2.建立makemigration資料夾,配置資料庫語言
class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Register', 實體類名稱
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('idCard', models.CharField(max_length=18)),
('pwd', models.CharField(max_length=55)),
],
options={
'db_table': 'new_tablename',
},
),
]


3.pycharm執行根據實體建立資料庫表
接下來要在pycharm的teminal中通過命令建立資料庫的表了。有2條命令,分別是:
python manage.py makemigration Django (後面指定app)
再輸入命令:python manage.py migrate

4.建立的資料庫表預設都是以dijango開頭
5.如果想重新執行makemigration Django建立表,需要刪除兩處地方
1)刪除makemigration 建立的檔案0001_initial.py
 1)刪除DELETE FROM `django_migrations` where app='Django'; app在資料庫中