#一、创建表
#1、setting.py
INSTALLED_APPS = [
#...
'myapp',
#...
]
#2、models.py
from django.db import models
class User(models.Model):
username = models.CharField(max_length=20,primary_key=True)
password = models.CharField(max_length=30)
email = models.EmailField()
phone = models.CharField(max_length=15)
#3.项目根目录
python manage.py makemigrations [app] #检测models.py并把修改的部分存储为一次'迁移'
python manage.py sqlmigrate [app] [0001] #返回相对应的sql语句
python manage.py migrate #将迁移应用到数据库上
#二、使用mysql
#命令行
pip3 install mysql-connector-python
#setting.py
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'test190727', # 数据库名
'USER': 'root', # 用户名
'PASSWORD': '', # 密码
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
三、管理页面
python manage.py createsuperuser
#admin.py加入应用的表
from django.contrib import admin
from app.models import Table
admin.site.register(Table)