父序列化器:
代码语言:javascript复制class ReadDeptSerializer(serializers.ModelSerializer):
id = serializers.IntergerField()
children = ChildDeptSerializer(many=True)
class Meta:
model = Dept
exclude = ['company','parent']
depth = 1
子序列化器
代码语言:javascript复制class ChildDeptSerializer(serializers.ModelSerializer):
children = serializers.SerializerMethodField()
class Meta:
model = Dept
depth = 1
exclude = ['company']
def get_children(self,obj):
if obj.children:
return childDeptSerializer(obj.children,many=True).data
return None