• 实验楼表结构序列化


    实验楼表序列化

    图片实列:

    model

    from utils.MyBaseModel import Base
    from django.db import models
    #
    
    # Create your models here.
    # 课程类别
    class CourseType(Base):
        title = models.CharField('课程类别', max_length=32)
        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=32)
        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.CASCADE, 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
    
    # zhang
    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
    # jiebiao
    class Sections(Base):
        title = models.CharField('节标题', max_length=32)
        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_table'
        def __str__(self):
            return self.title
    
    
    class JieDuan(Base):
        title = models.CharField('阶段名', max_length=24)
        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)
    

      

      序列化

    from rest_framework import serializers
    from course.models import *
    # 课程
    class Courseviewset(serializers.ModelSerializer):
        course_type = serializers.CharField(source='course_type.title')  # 正向查询
        # chapters_set = ChaptersSerializer(many=True)
        class Meta:
            model=Course
            fields='__all__'
    # 标签课程 class CourseTagSerializers(serializers.ModelSerializer): class Meta: model=CourseTag fields='__all__'
    #节表 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 CourseTypeSerializers(serializers.ModelSerializer): course_set= CourseCourseSerializers(many=True) class Meta: model=CourseType fields='__all__' # 单个课程继承 class CourseDeepSerializer(Courseviewset):
      chapters=ChaptersSerializer(many=True) # 序列化章表

    # **************************************************************************************  
    class JieduanSerializers(serializers.ModelSerializer):

       courses=CourseCourseSerializers(many=True)
       class Meta:
       model=JieDuan
       fields='__all__'
     class PathSerializers(serializers.ModelSerializer):
       # jieduan= JieduanSerializers(many=True)
       class Meta:
         model=Path
         fields='__all__'
         # fields=('id','title','img','desc','course_total')

     class JieDeepSerializers(serializers.ModelSerializer):

     jieduan=JieduanSerializers(many=True)
     class Meta:
     model=Path
     fields='__all__'
     # exclude=['user']












    urls

    # -*- coding: utf-8 -*-
    from django.contrib import admin
    from django.urls import path,include
    from course import views
    from rest_framework.routers import DefaultRouter
    routed=DefaultRouter()
    routed.register(r'type',views.CourseTypeViewSet)
    routed.register(r'tag',views.CourseTagViewSet)
    routed.register(r'course',views.CourseViewSet)
    routed.register(r'sections',views.SectionsViewSet)
    routed.register(r'chapters',views.ChapteViewSet)
    routed.register(r'path',views.PathViewSet)
    urlpatterns = [
    
    ]
    urlpatterns+=routed.urls
    

      

  • 相关阅读:
    code review
    设计原则
    知识点介绍
    REST API
    第四章 模块化React和Redux应用
    第3章 从Flux到Redux
    第二章 设计高质量的React组件
    React和Jquery比较
    第一章 React新的前端思维方式
    封装一个获取module.exports内容的方法
  • 原文地址:https://www.cnblogs.com/zhangshijiezsj/p/13808473.html
Copyright © 2020-2023  润新知