添加了外键moddle,查询时获取外键的 数据信息
遍历嵌套序列化
设置depth = 1表示遍历序列化深度为1,应该设置一个整数n,表示遍历到第n层,不设置遍历只显示外键ID,设置了会显示外键的详细信息
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = '__all__' # 序列化所有字段
# fields = ['student_no', 'student_name', 'student_sex', 'student_birthday', 'course', ] # 表示序列化指定字段
# exclude = ['student_name', 'student_sex', ] # 指定排除的字段
depth = 0
read_only_fields = ('student_no', 'student_sex') # 设置只读字段
设置后

设置前

https://www.cnblogs.com/rainbow-tan/p/16339712.html
添加外键的方法见: https://www.cnblogs.com/kaibindirver/p/16923586.html

浙公网安备 33010602011771号