django中的积累
只要继承了model.Model
, 就会生成一个新的表,但是,如果在Meta方法中添加abstract=True,就不会产生新的表,而是作为一个基类存放多个表共同拥有的方法和字段等
from django.db import models class Commen(models.Model): create_time = models.DateTimeField(auto_now_add=True) update_time = models.DateTimeField(auto_now=True) class Meta: # 添加关键字,不会产生新的表 abstract = True # 这张表继承了Base,哪怕没有字段create_time和update_time,也同样会展示出来 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
0.路径表关系梳理
1.路径表
# 路径表
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_path'
def __str__(self):
return self.title
2.阶段表
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)