注意:需要在项目中安装mysqlclient包
setting文件数据库相关修改:
python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite3',
'USER': 'root',
'PASSWORD': 'bai12345',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
什么是模型?:
ORM框架:
注册app需要在setting中添加:
python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'music',
'news',
'sport',
'bookstore'
]
python
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField("书名", max_length=100)
author = models.DecimalField('价格', max_digits=10, decimal_places=2)
执行后结果: 生成的表名为app名_小写类名