Django数据库基本配置
1.首先修改Settings中数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'marvel',
'USER':'huotong',
'PASSWORD':'879662581',
'HOST':'localhost',
'PORT':'3306',
}
}
2.在app中的__init__添加代码(或者在django_site中的init文件)
import pymysql
pymysql.install_as_MySQLdb()
3.创建数据表(在app的model.py中)
class Book(models.Model): #必须要继承的
nid = models.AutoField(primary_key=True) #自增id(可以不写,默认会有自增id)
title = models.CharField(max_length=32)
publishDdata = models.DateField() #出版日期
author = models.CharField(max_length=32)
price = models.DecimalField(max_digits=5,decimal_places=2) #一共5位,保留两位小数
执行命令,迁移到数据库
python3 manage.py makemigrations 创建脚本
python3 manage.py migrate 迁移
创建完成后的脚步文件
blog/migrations/0001_initial.py
# Generated by Django 2.2 on 2019-05-02 07:38
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Book',
fields=[
('nid', models.AutoField(primary_key=True, serialize=False)),
('title', models.CharField(max_length=32)),
('publishDdata', models.DateField()),
('author', models.CharField(max_length=32)),
('price', models.DecimalField(decimal_places=2, max_digits=5)),
],
),
]