Python marshmallow tree structure with polymorphism

后端 未结 1 825
后悔当初
后悔当初 2021-01-25 08:56

I have the following code for a tree structure:

class Node:
    def __init__(self, node_id: str):
        self.node_id = node_id
        self.children = []

             


        
1条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-25 09:43

    I found about marshmallow-oneofschema

    Although not integrated to the core, I think marshmallow-oneofschema is the recommended way to do polymorphism.

    this leads to a circular import

    You may pass the name as string to solve the circular import issue.

        children = fields.Nested('SomeNodeSchema', many=True)
    

    0 讨论(0)
提交回复
热议问题