# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
# Create your models here.
from django.contrib.auth.models import AbstractUser # 导入需要继承的默认字段
class UserProfile(AbstractUser):
"""添加用户表自定义的字段"""
nick_name = models.CharField(max_length=50,verbose_name=u'昵称',default='') # 长度50 默认值为''
birday = models.DateField(verbose_name=u'生日',null=True,blank=True) # DateField字段,可以为空
gender = models.CharField(max_length=5,choices=(('male',u'男'),('female',u'女')),default='female') # 因为性别只能为两种,所以我们用choices,并把默认值设置为female
address = models.CharField(max_length=100,verbose_name=u'地址',default='')
mobile = models.CharField(max_length=11,verbose_name=u'手机',null=True,blank=True)
image = models.ImageField(upload_to='image/%Y/%m',default=u'image/default.png',max_length=100) # 图片类型用ImageField类型,upload_to是储存地址,default是默认头像
# 因为我们用到了ImageField类型,这个类型依赖于另外一个库,所以我们还需要导入Pillow库(需要进入虚拟环境中安装) pip install pillow
# 上面已经定义完表的字段,下面我们要定义Meta信息
class Meta:
verbose_name = u'用户信息'
verbose_name_plural = verbose_name # 这是为了防止显示的时候多出一个S
# 下面还需要重载Unicode方法,如果不重载的话就不能打印自定义的字符串
def __unicode__(self):
return self.username
# 因为是继承了AbstractUser,所以可以直接获取self.username
# 添加好以上信息后我们还需要进入settings.py文件注册app和重载方法 AUTH_USER_MODEL = 'users.UserProfile'
# makemigrations users >>> migrate users (生成数据表) >>> yes (询问下面这些表是否删除,键入yes,生成成功)
class EmailVerifyRecord(models.Model):
"""邮箱验证相关"""
code = models.CharField(max_length=20,verbose_name=u"验证码")
email = models.CharField(max_length=50,verbose_name=u"邮箱")
# 以上两项都不能为空
send_type = models.CharField(choices=(("register",u"注册"),("forget",u"找回密码")),max_length=10)
# send_type 注册和找回密码时需要用到的
send_time = models.DateField(default=datetime.now)
# 获取当前时间,datetime.now去掉后面的括号表示以实例化的时间来获取
class Meta:
verbose_name = u"邮箱验证码"
verbose_name_plural = verbose_name
class Banner(models.Model):
"""轮播图相关的数据表"""
title = models.CharField(max_length=100,verbose_name=u"标题")
# 用于存储轮播图的标题
image = models.ImageField(max_length=100,upload_to="banner/%Y/%m",verbose_name=u"轮播图")
# 用于存储轮播图的图片路径信息
url = models.URLField(max_length=200,verbose_name=u"访问地址")
# 用于存储点击轮播图后的访问地址url
index = models.IntegerField(default=100,verbose_name=u"顺序")
# 用于存储播放顺序,默认100需要靠前就改成小一点的数字即可
add_time = models.DateField(default=datetime.now,verbose_name=u"添加时间")
# 用于存储添加轮播图的时间
class Meta:
verbose_name = u"轮播图"
verbose_name_plural = verbose_name
我们上面新增了:
EmailVerifyRecord
Banner
两个表,下面我们需要新建课程类app应用和表
===========================================================================
【新建courses的app应用】
pycharm 菜单栏 >>> Tools >>> Run manage.py Task >>>
manage.py@MxOnline> startapp courses [destination] (新建一个名称为users的app应用)
courses主要用于存放课程类的表
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
# Create your models here.
class Course(models.Model):
"""用于存储课程学习的表"""
name = models.CharField(max_length=50,verbose_name=u"课程名")
desc = models.CharField(max_length=300,verbose_name=u"课程描述")
detail = models.TextField(verbose_name=u"课程详情")
# 这里因为我们要做富文本,所以不限制长度
degree = models.CharField(max_length=2,choices=(("cj","初级"),("zj","中级"),("gj","高级")))
learn_times = models.IntegerField(default=0,verbose_name=u"学习时长(分钟)")
students = models.IntegerField(default=0,verbose_name=u"学习人数")
fav_nums = models.IntegerField(default=0,verbose_name=u"收藏人数")
image = models.ImageField(max_length=100,upload_to="courses/%Y/%m",verbose_name=u"封面图")
click_nums = models.IntegerField(default=0,verbose_name=u"点击数")
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"课程"
verbose_name_plural = verbose_name
class Lesson(models.Model):
"""章节表 涉及外键映射"""
course = models.ForeignKey(Course,verbose_name=u"课程章节")
# 上面需要外键指向Course课程信息表
name = models.CharField(max_length=100,verbose_name=u"章节名")
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"章节"
verbose_name_plural = verbose_name
class Video(models.Model):
"""视频信息表"""
lesson = models.ForeignKey(Lesson,verbose_name=u"章节")
# lesson外键指向Lesson
name = models.CharField(max_length=100, verbose_name=u"视频名")
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"视频"
verbose_name_plural = verbose_name
class CourseResource(models.Model):
"""视频资源信息表"""
course = models.ForeignKey(Course,verbose_name=u"课程")
# course外键指向Course
name = models.CharField(max_length=100, verbose_name=u"名称")
download = models.FileField(max_length=100,upload_to="course/resource/%Y/%m",verbose_name=u"资源文件")
# 因为是上传的资源,所以我们用FIleField类型
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")
class Meta:
verbose_name = u"课程资源"
verbose_name_plural = verbose_name
这样我们 courses 的app数据表就设置完成了,下面将要设置机构信息
知识重点:
1.富文本的数据类型
2.外键的指向类型
3.文件上传类型
================================================================================================
【课程机构数据表设计】
pycharm 菜单栏 >>> Tools >>> Run manage.py Task >>>
manage.py@MxOnline> startapp organizaiton [destination] (新建一个名称为organizaiton的app应用)
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
# Create your models here.
class CityDict(models.Model):
"""城市信息"""
name = models.CharField(max_length=20,verbose_name=u"城市")
desc = models.CharField(max_length=200,verbose_name=u"描述")
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = u"城市信息"
verbose_name_plural = verbose_name
class CourseOrg(models.Model):
"""机构信息"""
name = models.CharField(max_length=50,verbose_name=u"机构名称")
desc = models.TextField(verbose_name=u"机构描述")
click_nums = models.IntegerField(default=0,verbose_name=u"点击数")
fav_nums = models.IntegerField(default=0,verbose_name=u"收藏数")
image = models.ImageField(upload_to="org/%Y/%m",verbose_name=u"封面图")
address = models.CharField(max_length=150,verbose_name=u"机构地址")
city = models.ForeignKey(CityDict,verbose_name=u"所在城市")
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = u"机构信息"
verbose_name_plural = verbose_name
class Teacher(models.Model):
"""教师信息"""
org = models.ForeignKey(CourseOrg,verbose_name=u"所属机构")
name = models.CharField(max_length=50,verbose_name=u"教师名")
work_years = models.IntegerField(default=0,verbose_name=u"工作年限")
work_company = models.CharField(max_length=50,verbose_name=u"就职公司")
work_position = models.CharField(max_length=50,verbose_name=u"公司职位")
points = models.CharField(max_length=50,verbose_name=u"教学特点")
click_nums = models.IntegerField(default=0, verbose_name=u"点击数")
fav_nums = models.IntegerField(default=0, verbose_name=u"收藏数")
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = u"教师"
verbose_name_plural = verbose_name
到这里我们的分类数据表就设置完成了,下一章我们将建立数据表的顶层设计
pycharm 菜单栏 >>> Tools >>> Run manage.py Task >>>
manage.py@MxOnline> startapp operation [destination] (新建一个名称为operation的app应用)
# _*_ encoding:utf-8 _*_
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
from users.models import UserProfile
from courses.models import Course
# Create your models here.
class UserAsk(models.Model):
"""用户咨询"""
name = models.CharField(max_length=20,verbose_name=u"姓名")
mobile = models.CharField(max_length=11,verbose_name=u"手机")
course_name = models.CharField(max_length=50,verbose_name=u"课程名")
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"用户咨询"
verbose_name_plural = verbose_name
class CourseComments(models.Model):
"""用户课程评论"""
user = models.ForeignKey(UserProfile,verbose_name=u"用户")
course = models.ForeignKey(Course,verbose_name=u"课程")
comments = models.CharField(max_length=200,verbose_name=u"评论")
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"课程评论"
verbose_name_plural = verbose_name
class UserFavorite(models.Model):
"""用户收藏"""
user = models.ForeignKey(UserProfile,verbose_name=u"用户")
fav_id = models.IntegerField(default=0,verbose_name=u"数据id")
# fav_id 采用的是直接指向数据ID的方法,从而节省空间和逻辑判断等
fav_type = models.IntegerField(choices=((1,"课程"),(2,"课程机构"),(3,"讲师")),verbose_name=u"收藏类型")
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"收藏时间")
class Meta:
verbose_name = u"用户收藏"
verbose_name_plural = verbose_name
class UserMessage(models.Model):
"""用户信息"""
user = models.IntegerField(default=0,verbose_name=u"接收用户")
# 这里我们也用int类型是因为0可以表示发送给全部用户,而其他数字则表示发送给指定的用户id
message = models.CharField(max_length=500,verbose_name=u"消息内容")
has_read = models.BooleanField(default=False,verbose_name=u"是否已读")
# 这里新学了Bool类型数据,我们将信息先设置为未读即:False
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"用户消息"
verbose_name_plural = verbose_name
class UserCourse(models.Model):
"""用户课程"""
usre = models.ForeignKey(UserProfile,verbose_name=u"用户")
course = models.ForeignKey(Course,verbose_name=u"课程")
add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加时间")
class Meta:
verbose_name = u"用户课程"
verbose_name_plural = verbose_name
知识重点:
1.通过id查找
2.Bool数据类型的使用
================================================================================================
设计好数据表之后我们还需要将表创建到Mysql当中去
pycharm 菜单栏 >>> Tools >>> Run manage.py Task >>>
(当然,在执行命令前记得去settings.py中注册app文件夹名称,不然不能生成)
manage.py@MxOnline> makemigrations [appneme] (这里不输入app名称就表示生成全部的表)
下面我们需要将各个app应用都存放到apps文件夹当中,新建python Package文件夹命名apps(Directory不会生成init的py哦)
然后将四个app应用直接拖放到apps文件夹当中,不要勾选Search for references和Open moved files in editor,不然代码中会自动在form导入的文件夹名称前自动加上apps
拖放到apps文件夹以后IDE导入的模块会被标示红色,因为路径变化找不到该模块了,所以我们可以直接在IDE上将apps文件夹Mark Directory as >>> Sources Root (设置成蓝色,相当于告诉IDE可以将这个文件夹设置成为根目录),但是这个方法只是治标不治本的,只能在IDE中识别,所以我们还需要另外一个包来让python也能识别。
settings.py 配置文件
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
新增:
import os
import sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,os.path.join(BASE_DIR,'apps')) # 0表示插入的位置,第二个是路径(配置中的默认路径,apps文件夹)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
另外值得注意的一点是:配置的虚拟环境,变量也要设置虚拟环境的路径,并且把虚拟环境的路径设置在python解释器的前面,不然会先进入python本身的解释器中。