程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django -- MySQL database reflects the mapping data model to models

編輯:Python

Because just use models To define a table is too much trouble
This article is mainly about mysql Create a table in and then directly in models Generate code in
step1:
stay mysql Create a table


step2:
stay cmd Input in python manage.py inspectdb>polls/models.py among polls Created for you app name
step3:
Click on models.py That is, the generation is complete

Attached separately models The code written in maps to MySQL The method in
step1:
Enter the name of the table you want to create

class StudentInfo(models.Model):
stu_id = models.CharField(primary_key=True, max_length=20)
stu_name = models.CharField(max_length=20)
stu_psw = models.CharField(max_length=20)

step2:
cmd Input in python manage.py makemigrations polls(polls For you app name )
The successful result is shown in the figure

step3:
cmd Input in python manage.py migrate
Refresh MySQL In the table
The addition is complete


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved