从今天开始通过慕课网:强力Django+ 杀手级xadmin开发在线教育网站 (全新开发) 这套教程来从零开始实现搭建一个在线教育网站
第一天:
- 创建一个django项目(使用django2.2版本)
- 明确项目app
- 创建对应的数据库
创建django项目
https://www.cnblogs.com/hellosen/p/12402362.html
明确项目app
通过对网站需求的理解,一共创建了四个app
users:用户操作
operation:教育机构
organizations:用户的一些操作如收藏,点评等
courses:课程
创建对应的数据库
这是今天学习的重点
首先我们先明确一个结构,分层结构
在网站数据设计的时候首先要明确一个底层的数据库,其他app可以调用它,但是他不能调用他的上级,只能调用它同级的数据库,在最顶的app创建的数据库可以对整个apps进行调用。这样的好处就是使整个程序的数据库设计更加的简洁
在这个网站中 users 是最低成的apps他不能调用其他apps 而operation和course这个两个之间可以相互下调用并调用users表但是他们两个不能调用organizations 这个app 而organizations 可以调用下面的所有app
好了上面的注意点说好了 下面是实际操作了
首先是users app
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
# AbstractUser 我们继承django中自带的一个用户表然后又重新写了一些需要的字段
class UserProfile(AbstractUser):
nick_name = models.CharField(max_length=50,verbose_name='昵称',default="")
birthday = models.DateField(verbose_name='生日',null=True,blank=True)
genderChoice=((1,'男'),
(2,'女'),
(3,'保密'))
gender = models.IntegerField(choices=genderChoice,verbose_name='性别',default=3)
address = models.CharField(max_length=100,verbose_name='地址',default='')
mobile = models.CharField(max_length=11,verbose_name='手机号',unique=True)
image = models.ImageField(unique='head_image/%Y/%m',verbose_name='用户头像',default='head_image/default.jpg')
class Meta:
verbose_name='用户信息'
verbose_name_plural=verbose_name
db_table = 'Users'
def __str__(self):
if self.nick_name:
return self.nick_name
else:
return self.username
'''
这个BaseModel他只有一个字段就是增加时间
后面的所有model都有一个增加时间字段这里
我们就可以先写一个让其他几个model继承他
'''
class BaseModel(models.Model):
add_time = models.DateTimeField(default=datetime,verbose_name='添加时间')
class Meta:
abstract = True # 设置不生成表
这里我们注意一点上面的的我设置设置的user表 我们要在setting.py 中写下
AUTH_USER_MODEL = "users.UserProfile"
用来告诉django用户表是哪一个。
第二个是courses app
from datetime import datetime
from django.db import models
from apps.users.models import BaseModel
from apps.operation.models import Teacher
# Create your models here.
class Course(BaseModel):
teacher = models.ForeignKey(Teacher,on_delete=models.CASCADE,verbose_name='授课讲师')
name = models.CharField(max_length=50, verbose_name='课程名称')
desc = models.CharField(max_length=300, verbose_name='课程描述')
learn_times = models.IntegerField(verbose_name='视频时长')
degreeChoices = ((0, '初级'), (1, '中级'), (2, '高级'))
degree = models.IntegerField(verbose_name='难度', choices=degreeChoices, default=0)
category = models.CharField(default=u'后端开发', max_length=120, verbose_name='课程类别')
students = models.IntegerField(default=0, verbose_name='学习人数')
fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
click_nums = models.IntegerField(default=0, verbose_name='点击数')
tag = models.CharField(default="", verbose_name='课程标签', max_length=10)
youneed_know = models.CharField(default="", max_length=300, verbose_name='课程须知')
teacher_tell = models.CharField(default="", max_length=300, verbose_name='老师告诉你')
image = models.ImageField(upload_to='course/%Y/%m', verbose_name='封面图片', default='course/default.jpg')
class Meta:
verbose_name = '课程信息'
verbose_name_plural = verbose_name
db_table = 'course'
def __str__(self):
return self.name
class Lesson(BaseModel):
course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程名称')
name = models.CharField(max_length=100, verbose_name='章节名称')
learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟)')
class Meta:
verbose_name = '章节信息'
verbose_name_plural = verbose_name
db_table = 'Lesson'
def __str__(self):
return self.name
class Video(BaseModel):
lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
name = models.CharField(max_length=100, verbose_name=u'视频名')
learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟)')
url = models.CharField(max_length=200, verbose_name='访问地址')
class Meta:
verbose_name = '视频'
verbose_name_plural = verbose_name
db_table = 'Video'
def __str__(self):
return self.name
class CourseResource(BaseModel):
course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
name = models.CharField(max_length=100, verbose_name=u'课程资源')
file = models.FileField(upload_to='course/resourse/%Y/%m', verbose_name='下载地址', max_length=200)
class Meta:
verbose_name = '课程资源'
verbose_name_plural = verbose_name
db_table = 'CourseResource'
第三个是organizations app
from django.db import models
from apps.users.models import BaseModel
# Create your models here.
class City(BaseModel):
name = models.CharField(max_length=50, verbose_name='城市')
desc = models.CharField(max_length=200, verbose_name='描述')
class Meta:
verbose_name = '城市'
verbose_name_plural = verbose_name
db_table = 'City'
class CourseOrg(BaseModel):
name = models.CharField(max_length=50, verbose_name='机构名称')
desc = models.TextField(verbose_name='描述')
tag = models.CharField(default='全国知名', max_length=10, verbose_name='机构标签')
categoryChoices = ((0, '培训机构'), (1, '个人'), (2, '高校'))
category = models.IntegerField(default=0, choices=categoryChoices, verbose_name='机构类型')
fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
click_nums = models.IntegerField(default=0, verbose_name='点击数')
image = models.ImageField(upload_to='org/%Y/%m', verbose_name=u'logo', max_length=50, default='org/default.jpg')
address = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name=u'机构地址')
students = models.IntegerField(default=0, verbose_name='学习人数')
course_nums = models.IntegerField(default=0, verbose_name='课程数量')
class Meta:
verbose_name = '课程机构'
verbose_name_plural = verbose_name
db_table = 'CourseOrg'
class Teacher(BaseModel):
org = models.ForeignKey(CourseOrg, on_delete=models.CharField, verbose_name='所属机构')
name = models.CharField(max_length=50, verbose_name='教师名称')
work_years = models.IntegerField(default=0, verbose_name='工作年限')
work_company = models.CharField(max_length=50, verbose_name='就职公司')
work_position = models.CharField(max_length=50, verbose_name='公司职位')
points = models.CharField(max_length=50, verbose_name='教学特点')
fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
click_nums = models.IntegerField(default=0, verbose_name='点击数')
age = models.IntegerField(default=18, verbose_name='年龄')
image = models.ImageField(upload_to='teacher/%Y/%m', verbose_name=u'头像', max_length=50,
default='teacher/default.jpg')
class Meta:
verbose_name = '课程机构'
verbose_name_plural = verbose_name
db_table = 'Teacher'
第三个是organizations app
from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
from apps.users.models import BaseModel
from apps.courses.models import Course
User = get_user_model()
class UserAsk(BaseModel):
name = models.CharField(max_length=20,verbose_name='姓名')
mobile = models.CharField(max_length=11,verbose_name='手机号')
course_name = models.CharField(max_length=50,verbose_name='课程名称')
class Meta:
verbose_name = '用户咨询'
verbose_name_plural =verbose_name
db_table = 'UserAsk'
class CourseComments(BaseModel):
user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
course =models.ForeignKey(Course,on_delete=models.CASCADE,verbose_name='课程')
comments = models.CharField(200,verbose_name='评论')
class Meta:
verbose_name = '课程评论'
verbose_name_plural =verbose_name
db_table = 'CourseComments'
class UserFavorite(BaseModel):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
fav_id = models.IntegerField(verbose_name='数据id')
typeChoices = ((1,'课程'),(2,'课程机构'),(3,'讲师'))
fav_type = models.IntegerField(choices=typeChoices,default=1,verbose_name='收藏类型')
class Meta:
verbose_name = '用户收藏'
verbose_name_plural =verbose_name
db_table = 'UserFavorite'
class UserMessage(BaseModel):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
message = models.CharField(max_length=200,verbose_name='消息内容')
is_read = models.BooleanField(default=False,verbose_name='是否已读')
class Meta:
verbose_name = '用户消息'
verbose_name_plural =verbose_name
db_table = 'UserMessage'
class UserCourse(BaseModel):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
class Meta:
verbose_name = '用户课程'
verbose_name_plural =verbose_name
db_table = 'UserCourse'