mkvirtualenv -p python3 sixstar
pip install django==2.2
Navicat工具
.
链接:https://pan.baidu.com/s/1DuvGFy-q4bQV6lLCoSzMLQ?pwd=tuw5
提取码:tuw5
create database sixstar_2201 charset=utf8;
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 数据库引擎
'HOST': '127.0.0.1', # 数据库主机
'PORT': '3306', # 端口号
'USER': 'root', # 数据库用户账号
'PASSWORD': 'root', # 账号密码
'NAME': 'sixstar_2201', # 数据库名字
}
}
把下载好的文件 mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl(此mysqlclient对应python3.7版本) 放到刚创建的虚拟环境文件夹中
pycharm终端,安装 mysqlclient
mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl 百度网盘链接:
.
链接:https://pan.baidu.com/s/1ffUz257sThTuwH9i2LC51A?pwd=zkcu 提取码:zkcu
pip install mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl
cd apps
python ../manage.py startapp users
python ../manage.py startapp courses
python ../manage.py startapp organizations
python ../manage.py startapp operations
'apps.courses.apps.CoursesConfig', # 课程模块
'apps.users.apps.UsersConfig', # 用户模块
'apps.organizations.apps.OrganizationsConfig', # 机构模块
'apps.operations.apps.OperationsConfig', # 用户操作模块
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
# 枚举
GENDER_CHOICES = (
('male', '男'),
('female', '女')
)
class UserProfile(AbstractUser):
"""用户模型类"""
nick_name = models.CharField(max_length=50, verbose_name='名称', default='')
birthday = models.DateField(verbose_name='生日', null=True, blank=True)
gender = models.CharField(verbose_name='性别', choices=GENDER_CHOICES, max_length=6)
mobile = models.CharField(verbose_name='手机号码', unique=True, max_length=11)
# upload_to设置图片上传位置,相对media子目录
image = models.ImageField(verbose_name='用户头像', upload_to='head_image/%Y/%m', default='default.jpg')
class Meta:
verbose_name = '用户信息' # 自定义别名
verbose_name_plural = verbose_name # 自定义别名复数
def __str__(self):
if self.nick_name:
return self.nick_name
else:
return self.username
# 指定本项目用户模型类
AUTH_USER_MODEL = 'users.UserProfile'
pip install pillow
makemigrations
migrate
from datetime import datetime
class BaseModel(models.Model):
add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
class Meta:
abstract = True # 设置为抽象类,在迁移时就不会迁移这个类
from django.db import models
from apps.users.models import BaseModel
# 1.设计表结构有几个重要的点
""" 实体1<关系>实体2
课程 章节 视频 课程资源
"""
# 2. 实体的具体字段
# 3. 每个字段的类型,是否必填
class Course(BaseModel):
"""课程模型类"""
name = models.CharField(verbose_name="课程名", max_length=50)
desc = models.CharField(verbose_name="课程描述", max_length=300)
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分数)")
degree = models.CharField(verbose_name="难度", choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
students = models.IntegerField(default=0, verbose_name='学习人数')
fav_nums = models.IntegerField(default=0, verbose_name='收藏人数')
click_nums = models.IntegerField(default=0, verbose_name="点击数")
category = models.CharField(default=u"后端开发", max_length=20, 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="老师告诉你")
detail = models.TextField(verbose_name="课程详情")
image = models.ImageField(upload_to="courses/%Y/%m", verbose_name="封面图", max_length=100)
class Meta:
verbose_name = "课程信息"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Lesson(BaseModel):
"""章节模型类"""
# on_delete表示对应的外键数据被删除后,当前的数据应该怎么办
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="学习时长(分钟数)")
class Meta:
verbose_name = "课程章节"
verbose_name_plural = verbose_name
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=1000, verbose_name=u"访问地址")
class Meta:
verbose_name = "视频"
verbose_name_plural = verbose_name
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
def __str__(self):
return self.name
from django.db import models
from apps.users.models import BaseModel
# Create your models here.
class City(BaseModel):
"""城市模型类"""
name = models.CharField(max_length=20, verbose_name=u"城市名")
desc = models.CharField(max_length=200, verbose_name=u"描述")
class Meta:
verbose_name = "城市"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
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="机构标签")
category = models.CharField(default="pxjg", verbose_name="机构类别", max_length=4, choices=(("pxjg", "培训机构"), ("gr", "个人"), ("gx", "高校")))
click_nums = models.IntegerField(default=0, verbose_name="点击数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏数")
image = models.ImageField(upload_to="org/%Y/%m", verbose_name="logo", max_length=100)
address = models.CharField(max_length=150, verbose_name="机构地址")
students = models.IntegerField(default=0, verbose_name="学习人数")
course_nums = models.IntegerField(default=0, verbose_name="课程数")
city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name="所在城市")
class Meta:
verbose_name = "课程机构"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Teacher(BaseModel):
"""教师模型类"""
org = models.ForeignKey(CourseOrg, on_delete=models.CASCADE, verbose_name="所属机构")
name = models.CharField(max_length=50, verbose_name=u"教师名")
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="教学特点")
click_nums = models.IntegerField(default=0, verbose_name="点击数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏数")
age = models.IntegerField(default=18, verbose_name="年龄")
image = models.ImageField(upload_to="teacher/%Y/%m", verbose_name="头像", max_length=100)
class Meta:
verbose_name = "教师"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
from apps.organizations.models import Teacher
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name='讲师')
from django.db import models
from django.contrib.auth import get_user_model
from apps.users.models import BaseModel
from apps.courses.models import Course
# Create your models here.
# 通过 get_user_model 函数获取用户模型类
UserProfile = 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
def __str__(self):
return "{name}_{course}({mobile})".format(name=self.name, course=self.course_name, mobile=self.mobile)
class CourseComments(BaseModel):
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
comments = models.CharField(max_length=200, verbose_name="评论内容")
class Meta:
verbose_name = "课程评论"
verbose_name_plural = verbose_name
def __str__(self):
return self.comments
class UserFavorite(BaseModel):
"""用户收藏模型类"""
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
fav_id = models.IntegerField(verbose_name="数据id")
fav_type = models.IntegerField(choices=((1, "课程"), (2, "课程机构"), (3, "讲师")), default=1, verbose_name="收藏类型")
class Meta:
verbose_name = "用户收藏"
verbose_name_plural = verbose_name
def __str__(self):
return "{user}_{id}".format(user=self.user.username, id=self.fav_id)
class UserMessage(BaseModel):
"""用户消息模型类"""
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, verbose_name="用户")
message = models.CharField(max_length=200, verbose_name="消息内容")
has_read = models.BooleanField(default=False, verbose_name="是否已读")
class Meta:
verbose_name = "用户消息"
verbose_name_plural = verbose_name
def __str__(self):
return self.message
class UserCourse(BaseModel):
"""用户课程模型类"""
user = models.ForeignKey(UserProfile, 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
def __str__(self):
return self.course.name
makemigrations
migrate
createsuperuser