class StudentsSerializer(serializers.ModelSerializer):
detail = StudentDetailSerializer()
class Meta:
model = Student
fields = ('sname','detail')
class ClassSerializer(serializers.ModelSerializer):
#此处可以使用related_name来关联上student表
class_related = StudentsSerializer(many=True)
class Meta:
model = Class
fields = ('cname','class_related')
class TeacherSerializer(serializers.ModelSerializer):
classes = ClassSerializer(many=True)
class Meta:
model = Teacher
fields = ('tname','classes')
视图: