其他分享
首页 > 其他分享> > Serializer 嵌套使用

Serializer 嵌套使用

作者:互联网

class TeacherSerializer(ModelSerializer):
    """讲师序列化器"""

    class Meta:
        model = Teacher
        fields = [
            'name', 'brief'
        ]


class CourseSerializer(ModelSerializer):
    """课程序列化器"""
    # teacher 必须是Course外键才可以嵌套
    teacher = TeacherSerializer()
    """
    如果讲师有多个使用many=True
    teacher = TeacherSerializer(many=True)
    """

    class Meta:
        model = Course
        fields = [
            'name', 'course_img', 'brief', 'status', 'course_category',
            'students', 'lessons', 'pub_lessons', 'price', 'teacher'
        ]

 

标签:ModelSerializer,course,class,TeacherSerializer,嵌套,使用,序列化,teacher,Serializer
来源: https://www.cnblogs.com/wtil/p/14961217.html