1. 程式人生 > 實用技巧 >1911A:課程相關5張表設計

1911A:課程相關5張表設計

1.課程主頁面3張表

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

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

2.課程章節表2張表

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

3.course/admin.py註冊後臺管理

from django.contrib import admin
from . import models

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)

4.遷移表

python manage.py makemigrations
python manage.py migrate