#课程标签
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
正向查询
class CourseCourseSerializers(serializers.ModelSerializer):
corse_type = serializers.CharField(source='course_type.title')
class Meta:
model=Course
fields='__all__'
反向查询
class CourseTypeSerializers(serializers.ModelSerializer):
course_set= CourseCourseSerializers(many=True) # 表明+set
class Meta:
model=CourseType
fields='__all__'