码迷,mamicode.com
首页 > 其他好文 > 详细

实验楼 实现课程模块

时间:2020-10-13 17:22:07      阅读:24      评论:0      收藏:0      [点我收藏+]

标签:other   esc   ram   name   delete   地址   status   orm   ocs   

首先开始配置settings

MEDIA_URL=/media/   #媒体文件的地址
MEDIA_ROOT=os.path.join(BASE_DIR,media)

1.model 模块

 

from django.db import models
from utils.MyBaseModel import Base

# Create your models here.
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,default=1,max_length=20)
    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)
    serual_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)
    serual_num = models.IntegerField(节序号)
    course = models.ForeignKey(Chapters, related_name=chapters, 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#迁移数据库  python  manage.py makemigrations      python  manage.py  migrate

2.admin 文件中注册

from django.contrib import admin
from . import models
# Register your models here.
admin.site.register(models.CourseType)
admin.site.register(models.CourseTag)
admin.site.register(models.Course)
admin.site.register(models.Sections)
admin.site.register(models.Chapters)

3.添加序列化

# -*- coding: utf-8 -*-
from   rest_framework import serializers
from course.models import *

class  CourseTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model=CourseType
        fields="__all__"



class CourseTagSerializer(serializers.ModelSerializer):
    class Meta:
        model=CourseTag
        fields="__all__"

class CourseSerializer(serializers.ModelSerializer):
    # course_type=serializers.CharField(source=course_type.title)
    class Meta:
        model=Course
        fields="__all__"
        deep= 1


class SectionsSerializer(serializers.ModelSerializer):
    class Meta:
        model=Sections
        fields="__all__"

class ChaptersSerializer(serializers.ModelSerializer):
    sections=SectionsSerializer(many=True)
    class Meta:
        model=Chapters
        fields="__all__"

4.添加视图类

# -*- coding: utf-8 -*-
from   rest_framework import serializers
from course.models import *

class  CourseTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model=CourseType
        fields="__all__"



class CourseTagSerializer(serializers.ModelSerializer):
    class Meta:
        model=CourseTag
        fields="__all__"

class CourseSerializer(serializers.ModelSerializer):
    # course_type=serializers.CharField(source=course_type.title)
    class Meta:
        model=Course
        fields="__all__"
        deep= 1


class SectionsSerializer(serializers.ModelSerializer):
    class Meta:
        model=Sections
        fields="__all__"

class ChaptersSerializer(serializers.ModelSerializer):
    sections=SectionsSerializer(many=True)
    class Meta:
        model=Chapters
        fields="__all__"

5.添加主路由,再子路由

"""syl URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path(‘‘, views.home, name=home)
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path(‘‘, Home.as_view(), name=home)
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path(blog/, include(blog.urls))
"""
from django.contrib import admin
from django.urls import path,include
from django.conf.urls.static import static
from  syl import settings
urlpatterns = [
    path(admin/, admin.site.urls),
    # path(user/, include(user.urls)),
    # path(verify/, include(verifications.urls)),
    # path(oauth/, include(oauth.urls)),
    path(course/,include(course.urls))
]

urlpatterns +=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)



#子路由
from django.urls import path,re_path
from rest_framework.routers import DefaultRouter

from . import views


router=DefaultRouter()
router.register(rtype,views.CourseTypeVIewSet)
router.register(rtag,views.CourseTagVIewSet)
router.register(rcourse,views.CourseVIewSet)
router.register(rsections,views.SectionsVIewSet)
router.register(rchapters,views.ChaptersVIewSet)

urlpatterns=[


]
urlpatterns +=router.urls

 

实验楼 实现课程模块

标签:other   esc   ram   name   delete   地址   status   orm   ocs   

原文地址:https://www.cnblogs.com/wangxudong01/p/13805754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!