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'
        ]

 

上一篇:android – Alpha动画无法正常工作


下一篇:drf-序列化器serializer