course/model.py
from django.db import models # Create your models here. # from utils.MyBaseModel import Base class Base(models.Model): create_time=models.DateTimeField('创建时间',auto_now_add=True,null=True) update_time=models.DateTimeField('更新时间',auto_now=True,null=True) class Meta: abstract=True class CourseType(Base): title=models.CharField('课程类别',max_length=16) sequence=models.IntegerField('展示顺序',default=10) class Meta: db_table='tb_coursetype' def __str__(self): return self.title class CourseTag(Base): title=models.CharField('课程标签',max_length=16) sequence=models.IntegerField('展示顺序',default=10) class Meta: db_table='tb_coursetag' def __str__(self): return self.title class Course(Base): STATUS=( ('0','即将上线'), ('1','已经上线'), ('2','已下线'), ) title=models.CharField('课程名',max_length=24) desc=models.CharField('课程描述',max_length=256) img=models.ImageField('课程logo',upload_to='course',null=True) course_type=models.ForeignKey(CourseType,verbose_name='课程类别',on_delete=models.SET_NULL,default=None,null=True) course_tag=models.ManyToManyField(CourseTag,verbose_name='课程标签') status=models.CharField('课程状态',choices=STATUS,max_length=8,default='1') attention=models.IntegerField('关注人数',default=0) learner=models.IntegerField('学习人数',default=0) class Meta: db_table='tb_course' def __str__(self): return self.title class Chapters(Base): title=models.CharField('章标题',max_length=24) serial_num=models.IntegerField('章序号') course=models.ForeignKey(Course,related_name='chapters',on_delete=models.SET_NULL,null=True) class Meta: db_table='tb_chapters' def __str__(self): return self.title class Sections(Base): title=models.CharField('节标签',max_length=24) serial_num=models.IntegerField('节序号') chapters=models.ForeignKey(Chapters,related_name='sections',on_delete=models.SET_NULL,null=True) learn_time=models.IntegerField('学习小时',default=1) video=models.FileField("上传视频",upload_to="videos/%y%m%d/",blank=True,max_length=1024) seq_num=models.IntegerField("序号",default=1) class Meta: db_table='tb_sections' def __str__(self): return self.title class Path(Base): title=models.CharField('路径名',max_length=16) img=models.ImageField('路径图片',upload_to='path',null=True) desc=models.CharField('路径描述',max_length=255) user=models.ManyToManyField('user.User',blank=True) def course_total(self): count=0 for jd in self.jieduan.all(): count=count+jd.courses.count() return count class Meta: db_table='tb_path' def __str__(self): return self.title class JieDuan(Base): title=models.CharField('阶段名',max_length=16) serial_num=models.IntegerField('路径序号') path=models.ForeignKey(Path,related_name='jieduan', on_delete=models.SET_NULL,null=True) courses=models.ManyToManyField(Course,blank=True) class Meta: db_table='tb_jieduan' def __str__(self): return "%s-第%s阶段-%s" % (self.path.title,self.serial_num,self.title)
course/urls.py
from django.urls import path,re_path from rest_framework.routers import DefaultRouter from . import views router=DefaultRouter() router.register(r'type',views.CourseTypeViewSet) router.register(r'tag',views.CourseTagViewSet) router.register(r'course',views.CourseViewSet) router.register(r'sections',views.SectionsViewSet) router.register(r'chapters',views.ChaptersViewSet) router.register(r'path',views.PathViewSet) urlpatterns = [ ] urlpatterns +=router.urls
course/views.py
from django.shortcuts import render # Create your views here. from django_filters.rest_framework import DjangoFilterBackend from rest_framework import viewsets from rest_framework.pagination import PageNumberPagination from course.models import * from course.serializers import * class PageNum(PageNumberPagination): page_size_query_param = 'page-size' class CourseTypeViewSet(viewsets.ModelViewSet): queryset = CourseType.objects.all() serializer_class=CourseTypeSerializer class CourseTagViewSet(viewsets.ModelViewSet): queryset = CourseTag.objects.all() serializer_class=CourseTagSerializer class CourseViewSet(viewsets.ModelViewSet): queryset=Course.objects.all() serializer_class=CoursesDeepSerializer class SectionsViewSet(viewsets.ModelViewSet): queryset = Sections.objects.all() serializer_class=SectionsSerializer class ChaptersViewSet(viewsets.ModelViewSet): queryset = Chapters.objects.all() serializer_class = ChaptersSerializer class PathViewSet(viewsets.ModelViewSet): queryset = Path.objects.all() serializer_class = PathSerializer def get_serializer_class(self): if self.action=="list": return PathSerializer else: return PathDeepSerializer
course/serializers.py
# -*- coding: utf-8 -*- from rest_framework import serializers from course.models import * class CourseTypeSerializer(serializers.ModelSerializer): class Meta: model=CourseType fields="__all__" class CourseSerializer(serializers.ModelSerializer): class Meta: model=Course fields="__all__" class CourseCourseSerializers(serializers.ModelSerializer): class Meta: model=Course fields="__all__" class CourseTagSerializer(serializers.ModelSerializer): course_set = CourseCourseSerializers(many=True) class Meta: model=CourseTag fields="__all__" class SectionsSerializer(serializers.ModelSerializer): # chapters=serializers.CharField(source='chapters.zdm') class Meta: model=Sections fields="__all__" class ChaptersSerializer(serializers.ModelSerializer): sections=SectionsSerializer(many=True) class Meta: model = Chapters fields = '__all__' class PathSerializer(serializers.ModelSerializer): class Meta: model=Path fields=('id','title','img','desc','course_total') class JieDuanSerializer(serializers.ModelSerializer): courses=CourseSerializer(many=True) class Meta: model=JieDuan fields="__all__" class PathDeepSerializer(serializers.ModelSerializer): jieduan=JieDuanSerializer(many=True) class Meta: model=Path exclude=['user'] class CoursesDeepSerializer(CourseCourseSerializers): chapters=ChaptersSerializer(many=True)
course/admin.py
from django.contrib import admin # Register your models here. from .import models admin.site.register(models.CourseType) admin.site.register(models.CourseTag) admin.site.register(models.Course) admin.site.register(models.Chapters) admin.site.register(models.Sections) admin.site.register(models.Path) admin.site.register(models.JieDuan)