父序列化器:
class ReadDeptSerializer(serializers.ModelSerializer):
id = serializers.IntergerField()
children = ChildDeptSerializer(many=True)
class Meta:
model = Dept
exclude = ['company','parent']
depth = 1
子序列化器
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
评论 (0)