Django打造在线教育平台_day_2:新建courses app 编写models

1、新建courses app,Tools 工具栏运行 run manage.py Task输入:

startapp courses 

2、分析courses app,需要建立4个表最上层是课程表,章节表,视频表,资源表,层级关系如下。

    课程表

  章节表  资源表  

  视频表  

3、编写courses/models.py

from datetime import datetime
from django.db import models

# Create your models here.

class Course(models.Model):
    name = models.CharField(max_length=50, verbose_name=u'课程名')
    desc = models.CharField(max_length=300, verbose_name=u'课程描述')

    # TextField 与 CharField的区别是TextField可以不限制文本长度
    detail = models.TextField(verbose_name=u'课程详情')
    degree = models.CharField(max_length=2 ,choices=(('cj',u'初级'),('zj',u'中级'),('gj',u'高级')))
    learn_times = models.IntegerField(default=0, verbose_name=u'学习时长(分钟数)')
    students = models.IntegerField(default=0, verbose_name=u'学习人数')
    fav_nums = models.IntegerField(default=0, verbose_name=u'收藏人数')
    image = models.ImageField(upload_to='courses/%Y%m', max_length=100, verbose_name=u'封面')
    click_nums = models.IntegerField(default=0, verbose_name=u'点击数')
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'课程'
        verbose_name_plural = verbose_name


class Lesson(models.Model):
    """课程表与章节表是1对多的关系需要使用外建来完成(django外建是用来完成1对多,多对多,多对1的关系)"""
    course = models.ForeignKey(Course, verbose_name=u'课程')   # 引入外建,完成1对多的关系
    name = models.CharField(max_length=100, verbose_name=u'章节名')
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'课程章节'
        verbose_name_plural = verbose_name


class Video(models.Model):
    lesson = models.ForeignKey(Lesson, verbose_name=u'章节')  # 引入外建,完成1对多的关系
    name = models.CharField(max_length=100, verbose_name=u'视频名')
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')

    class Meta:
        verbose_name = u'课程视频'
        verbose_name_plural = verbose_name


class CourseResource(models.Model):
    course = models.ForeignKey(Course, verbose_name=u'课程')   # 引入外建,完成1对多的关系
    name = models.CharField(max_length=100, verbose_name=u'资源名')
    add_time = models.DateTimeField(default=datetime.now, verbose_name=u'添加时间')
    download = models.FileField(upload_to='course/resource/%Y/%m', max_length=100, verbose_name=u'资源文件')

    class Meta:
        verbose_name = u'课程资源'
        verbose_name_plural = verbose_name

4、在MxOnline/settings.py中注册app

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',  # 注册App
    'operation',
    'courses ',
    'organization ',
]

 5、在数据库生成新定义的表,Tools 工具栏运行 run manage.py Task输入:

makemigrations 
migrate 
posted @ 2017-06-23 14:38  起航追梦人  阅读(551)  评论(0编辑  收藏  举报