py2neo ogm基础知识:混淆了如何设置模型/导入

时间:2019-09-18 00:57:01

标签: python neo4j py2neo

我正在尝试(但失败)使用py2neo v4。我本质上是在尝试使this guidance跨python文件工作。

在我的模型中,我有“设备”和“平台”。我将这两个对象分成了两个文件:

# platform.py
from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedFrom, Label
from .Equipment import Equipment

class Platform(GraphObject):
    __primarykey__ = "name"
    name = Property()
    quantity = Property()
    equipment = RelatedFrom(Equipment, "ENABLES")

    def as_json(self):
        self.printOut()
        res = {
            "name": self.name,
            "equipment": [b.name for b in self.equipment],
        }
        return res

    def __init__(self, name):
        super().__init__()
        self.name = name

# Equipment.py
from py2neo.ogm import GraphObject, Property, RelatedTo, RelatedFrom, Label
from .Platform import Platform

class Equipment(GraphObject):
    __primarykey__ = "name"
    name = Property()
    platforms = RelatedTo(Platform, "ENABLES")

    def __init__(self, name):
        super().__init__()
        self.name = name

我的目标是对设备进行迭代,并将链接的相应平台打印为json:

equipment_matches = Equipment.match(graph)
for equipment_match in equipment_matches:
    for platform_node in equipment_match.platforms:
        print(platform_node.as_json())

我目前在循环导入(在上面的代码中)之间徘徊: ImportError: cannot import name 'Platform' from 'ModuleName.Platform' 或者当我按照其他示例将Platform设置为Equipment.py中的字符串时,出现了另一个错误:AttributeError: module 'ModuleName.Equipment' has no attribute 'Platform'

我错过了什么吗,还是py2neo并不是要做我想做的事情?

编辑:到目前为止,我不确定的解决方案是在 init .py之内。我这样编辑类:

from py2neo.ogm import RelatedFrom, RelatedTo
from .Equipment import Equipment
from .Platform import Platform

Platform.equipment = RelatedFrom(Equipment, "ENABLES")
Equipment.platforms = RelatedTo(Platform, "ENABLES")

这行得通,但我想知道是否有更友好的方法

0 个答案:

没有答案