创建一个项目(mydemo):
django-admin startproject mydemo
cd webtest
python manage.py startapp book

?
打开 webtest/webtest 目录下的__init__.py 文件,
写入如下代码:
import pymysql
pymysql.install_as_MySQLdb()
再打开 settings.py 文件,修改 INSTALLED_APPS 以完成对应用的配置,如下:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
"book", # 添加 book 应用
]
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql', # MySQL 引擎
'NAME': "dbtest", # 数据库名
"USER": "root", # 用户名
"PASSWORD": "hy12345", # 密码
"HOST": "localhost", # 主机名
"PORT": 3306, # 端口号
}
}
from django.db import models
# Create your models here.
class BookInfo(models.Model):
# 图书名
bname = models.CharField(max_length=20)
# 发布日期
bpub_date = models.DateField()
# 页面数
bpage = models.IntegerField(default=0)
# 软删除标记
is_delete = models.BooleanField(default=False)
class AuthorInfo(models.Model):
# 作者名
aname = models.CharField(max_length=20)
# 性别
agender = models.BooleanField(default=True)
# 简介
acontent = models.CharField(max_length=100)
# 软删除标记
is_delete = models.BooleanField(default=False)
# 外键名
abook = models.ForeignKey("BookInfo")

 

?
?
?
?
?
|