Python Neo4j 自参考

Python Neo4j Self reference

我正在使用来自 https://github.com/robinedwards/neomodel . Documentation http://neomodel.readthedocs.org/en/latest/

的新模型库

我有 2 个 classes 实体和类别 - 每个类别都属于一个实体,每个实体可以有一个 parent_entity。对于类别 class,这是有效的:

class Category(StructuredNode):
    name = StringProperty(required=True)
    entity = RelationshipTo(Entity, 'BELONGS_TO', cardinality=One)
    created_at = DateTimeProperty()
    updated_at = DateTimeProperty()

但是对于实体 class 我写了:

class Entity(StructuredNode):
    name = StringProperty(required=True)
    image = StringProperty()
    description = StringProperty()
    parent_entity = Relationship(Entity, 'PARENT', cardinality=ZeroOrMore)
    categories = RelationshipFrom(Category, 'BELONGS_TO', cardinality=ZeroOrMore)
    created_at = DateTimeProperty()
    updated_at = DateTimeProperty()

这给我一个错误:

parent_entity = Relationship(Entity, 'PARENT', cardinality=ZeroOrMore)
NameError: name 'Entity' is not defined

如何实现自引用模型?任何信息都会非常有帮助提前致谢!

这是因为此时 class 实体尚未编译。如果将其更改为字符串 'Entity',它应该会按预期工作。