1、四个model完整代码:
# users/models.py
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
class UserProfile(AbstractUser):
gender_choices = (
('male', '男'),
('female', '女')
)
nick_name = models.CharField('昵称', max_length=50, default='')
birthday = models.DateField('生日', null=True, blank=True)
gender = models.CharField('性别', max_length=10, choices=gender_choices, default='female')
adress = models.CharField('地址', max_length=100, default='')
mobile = models.CharField('手机号', max_length=11, null=True, blank=True)
image = models.ImageField(upload_to='image/%Y%m', default='image/default.png', max_length=100)
class Meta:
verbose_name = '用户信息'
verbose_name_plural = verbose_name
def __str__(self):
return self.username
class EmailVerifyRecord(models.Model):
send_choices = (
('register', '注册'),
('forget', '找回密码')
)
code = models.CharField('验证码', max_length=20)
email = models.EmailField('邮箱', max_length=50)
send_type = models.CharField(choices=send_choices, max_length=10)
send_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = '邮箱验证码'
verbose_name_plural = verbose_name
class Banner(models.Model):
title = models.CharField('标题', max_length=100)
image = models.ImageField('轮播图', upload_to='banner/%Y%m', max_length=100)
url = models.URLField('访问地址', max_length=200)
index = models.IntegerField('顺序', default=100)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '轮播图'
verbose_name_plural = verbose_name
# course\models.py
from django.db import models
from datetime import datetime
# Create your models here.
class Course(models.Model):
name = models.CharField(max_length=50, verbose_name="课程名")
desc = models.CharField(max_length=300, verbose_name="课程描述")
detail = models.TextField(verbose_name="课程详情") # 暂时为TextField
degree = models.CharField(choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")), max_length=2)
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟数)")
students = models.IntegerField(default=0, verbose_name="学习人数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏人数")
image = models.ImageField(upload_to="course/%Y/%m", verbose_name="封面图", max_length=100)
click_num = models.IntegerField(default=0, verbose_name="点击数")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class Lesson(models.Model):
course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE) # 删除关联数据,与之关联也删除
name = models.CharField(max_length=100, verbose_name="章节名")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "章节"
verbose_name_plural = verbose_name
def __str__(self):
return '《{0}》课程的章节 >> {1}'.format(self.course, self.name)
class Video(models.Model):
lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
name = models.CharField(max_length=100, verbose_name="视频名称")
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "视频"
verbose_name_plural = verbose_name
class CourseResource(models.Model):
course = models.ForeignKey(Course, verbose_name="课程", on_delete=models.CASCADE)
name = models.CharField(max_length=100, verbose_name="视频名称")
download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name="资源文件", max_length=100)
add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")
class Meta:
verbose_name = "课程信息"
verbose_name_plural = verbose_name
# organization\models.py
from datetime import datetime
from django.db import models
class CityDict(models.Model):
name = models.CharField('城市', max_length=20)
desc = models.CharField('描述', max_length=200)
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = '城市'
verbose_name_plural = verbose_name
class CourseOrg(models.Model):
name = models.CharField('机构名称', max_length=50)
desc = models.TextField('机构描述')
click_nums = models.IntegerField('点击数', default=0)
fav_nums = models.IntegerField('收藏数', default=0)
image = models.ImageField('封面图', upload_to='org/%Y%m', max_length=100)
address = models.CharField('机构地址', max_length=150)
city = models.ForeignKey(CityDict, verbose_name='所在城市', on_delete=models.CASCADE)
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = '课程机构'
verbose_name_plural = verbose_name
class Teacher(models.Model):
org = models.ForeignKey(CourseOrg, verbose_name='所属机构', on_delete=models.CASCADE)
name = models.CharField('教师名', max_length=50)
work_years = models.IntegerField('工作年限', default=0)
work_company = models.CharField('就职公司', max_length=50)
work_position = models.CharField('公司职位', max_length=50)
points = models.CharField('教学特点', max_length=50)
click_nums = models.IntegerField('点击数', default=0)
fav_nums = models.IntegerField('收藏数', default=0)
add_time = models.DateTimeField(default=datetime.now)
class Meta:
verbose_name = '教师'
verbose_name_plural = verbose_name
def __str__(self):
return "[{0}]的教师: {1}".format(self.org, self.name)
# operation\models.py
from datetime import datetime
from django.db import models
from course.models import Course
from users.models import UserProfile
class UserAsk(models.Model):
name = models.CharField('姓名', max_length=20)
mobile = models.CharField('手机', max_length=11)
course_name = models.CharField('课程名', max_length=50)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '用户咨询'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class CourseComments(models.Model):
user = models.ForeignKey(UserProfile, verbose_name='用户', on_delete=models.CASCADE)
course = models.ForeignKey(Course, verbose_name='课程', on_delete=models.CASCADE)
comments = models.CharField('评论', max_length=200)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '课程评论'
verbose_name_plural = verbose_name
class UserFavorite(models.Model):
FAV_TYPE = (
(1, '课程'),
(2, '课程机构'),
(3, '讲师')
)
user = models.ForeignKey(UserProfile, verbose_name='用户', on_delete=models.CASCADE)
fav_id = models.IntegerField('数据id', default=0)
fav_type = models.IntegerField(verbose_name='收藏类型', choices=FAV_TYPE, default=1)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '用户收藏'
verbose_name_plural = verbose_name
class UserMessage(models.Model):
user = models.IntegerField('接受用户', default=0)
message = models.CharField('消息内容', max_length=500)
has_read = models.BooleanField('是否已读', default=False)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '用户消息'
verbose_name_plural = verbose_name
class UserCourse(models.Model):
user = models.ForeignKey(UserProfile, verbose_name='用户', on_delete=models.CASCADE)
course = models.ForeignKey(Course, verbose_name='课程', on_delete=models.CASCADE)
add_time = models.DateTimeField('添加时间', default=datetime.now)
class Meta:
verbose_name = '用户课程'
verbose_name_plural = verbose_name
2、创建完后记得把App添加到settings的“INSTALLED_APPS”里面
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'course',
'organization',
'operation',
]
3、迁移到数据库
makemigrations
migrate
4、将四个App放到一起
- 新建package:apps
- 把四个App都拖进去
红框内的 √ 记得去掉 - 然后Mark为sourceRoot
5、然而我们发现,在cmd下运行的话还是会报错
需要在settings设置:
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'))
再运行就可以了
未完待续~~~