• 课程模块


    课程模块构建

    1.在apps/course新建courseApp中

    # -*- coding: utf-8 -*
    from django.db import models
    
    
    # Create your models here.
    
    # 基类
    class Base(models.Model):
        create_time = models.TimeField(auto_now_add=True)
        update_time = models.TimeField(auto_now=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'
            verbose_name = '课程'
    
        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='video/%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=24)
        img = models.ImageField('路径logo', 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)
    
    

    2.在course/Serializers

    # -*- coding: utf-8 -*-
    from rest_framework import serializers
    from course.models import *
    
    
    # 节表
    class SectionsSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Sections
            fields = '__all__'
    
    
    # 章表
    class ChaptersSerializer(serializers.ModelSerializer):
        sections = SectionsSerializer(many=True)
    
        class Meta:
            model = Chapters
            fields = '__all__'
    
    
    # 课程表
    class CourseDeepSerializer(serializers.ModelSerializer):
         # 字段名,必须是模型,可以引用到的变量
         # Course()  'chaoters' 才能作为字段名 如果是集合需要加 many=Ture
        chapters = ChaptersSerializer(many=True)
    
        class Meta:
            model = Course
            fields = '__all__'
    
    
    class CourseSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Course
            fields = '__all__'
    
    
    # 课程分类
    class CourseTypeSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = CourseType
            fields = '__all__'
    
    
    # 课程标签
    class CourseTagSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = CourseTag
            fields = '__all__'
    
    
    # 路径
    class PathSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Path
            fields = '__all__'
    
    
    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
            fields = '__all__'
    
    

    3.在course/views

    from django.shortcuts import render
    from django_filters.rest_framework import DjangoFilterBackend
    from rest_framework import viewsets
    from rest_framework.filters import OrderingFilter
    from rest_framework.pagination import PageNumberPagination
    from rest_framework.viewsets import ModelViewSet
    from .models import *
    from .serializers import *
    # Create your views here.
    
    
    class PageNum(PageNumberPagination):
        page_size_query_param = 'page_size'
    
    
    class CourseTypeViewSet(ModelViewSet):
        queryset = CourseType.objects.all()
        serializer_class = CourseTypeSerializer
    
    
    class CourseTagViewSet(ModelViewSet):
        queryset = CourseTag.objects.all()
        serializer_class = CourseTagSerializer
    
    
    class CourseViewSet(ModelViewSet):
        queryset = Course.objects.all()
        serializer_class = CourseDeepSerializer
    
        # 指定过滤方法类,排序方法类,一个或多个
        # 同时支持过滤和排序
        filter_backends = (DjangoFilterBackend,OrderingFilter)
    
        # 指定排序字段,不设置,排序功能不起效
        ordering_fields = ('attention','learner','create_time')
    
        # 指定过滤字段,不设置,过滤功能不起效
        filter_fields = ('title','desc','status','course_type','course_tag')
    
        # 自定义分页器,覆盖全局配置
        pagination_class = PageNum
    
        def get_serializer_class(self):
            if self.action == 'list':
                return CourseSerializer
            else:
                return CourseDeepSerializer
    
    
    
    class SectionsViewSet(ModelViewSet):
        queryset = Sections.objects.all()
        serializer_class = SectionsSerializer
    
    
    class ChaptersViewSet(ModelViewSet):
        queryset = Chapters.objects.all()
        serializer_class = ChaptersSerializer
    
    
    #  路径视图
    class PathViewSet(ModelViewSet):
        queryset = Path.objects.all()
        serializer_class = PathDeepSerializer
    
        def get_serializer_class(self):
            if self.action == 'list':
                return PathSerializer
            else:
                return PathDeepSerializer
    
    

    4.使用course/admin

    from django.contrib import admin
    from course.models import *
    # Register your models here.
    
    model_list = [Course,CourseTag,CourseType,Chapters,Sections,Path,JieDuan]
    
    for model in model_list:
        admin.site.register(model)
    

    5.配置图片路径

    • 新建图片存储路径包
    • syl/settings
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR,'media')
    
    • syl/urls
    from django.conf.urls.static import static
    from syl import settings
    
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
    
  • 相关阅读:
    模块与包的导入
    递归
    day04
    装饰器2_根据认证来源判断用户和计算登录时间
    装饰器1_统计时间函数装饰欢迎登录函数
    tail -f a.txt | grep 'python'
    函数
    内置函数1
    python模块整理
    VBS恶作剧代码
  • 原文地址:https://www.cnblogs.com/Beginner-Y/p/13810691.html
Copyright © 2020-2023  润新知