Neo4j py2neo:使用对象获取关系属性

Neo4j py2neo:使用对象获取关系属性,neo4j,graph-databases,py2neo,Neo4j,Graph Databases,Py2neo,我正在使用图形对象映射,其中我有两个对象来表示两个节点。我还想为关系定义属性,如何为与属性的关系定义类? ,我看到了一个示例,但没有使用对象 我的代码如下所示: class Intent(GraphObject): name = Property() message = RelatedTo("Message", "HAS") class Message(GraphObject): name = Property() owner = RelatedFrom("Intent

我正在使用图形对象映射,其中我有两个对象来表示两个节点。我还想为关系定义属性,如何为与属性的关系定义类? ,我看到了一个示例,但没有使用对象

我的代码如下所示:

class Intent(GraphObject):
   name = Property()
   message = RelatedTo("Message", "HAS")

class Message(GraphObject):
   name = Property()
   owner = RelatedFrom("Intent","HAS")

graph = Graph(password='1234')
intent = Intent()
intent.name = "ABC"
complete_intent = graph.pull(intent)

如果我想为关系“HAS”拥有一些属性,我如何定义类HAS?基类应该从何处继承(比如节点的GraphObject,有关系的东西吗?

当前版本的
py2neo
不允许“关系对象”,这将允许您以标准的OOP-y方式将参数放在关系上

OGM对于启动项目和快速插入/检索事务非常有用,但对于大多数更复杂的事务,您需要编写密码来实现您的目标

但是,通常您可能希望在
GraphObject
上组织这些查询,有时您可以扩展
GraphObject
以添加一个方法来提供一些必需的功能

在您的用例中,您可能希望添加一些类似下面的方法来获取或设置关系属性。我已经编写了一个检索关系属性的快速演示,您也可以添加一个类似的方法来设置关系

from py2neo.ogm import (GraphObject,
                        INCOMING,
                        OUTGOING,
                        Property,
                        RelatedFrom,
                        RelatedTo,)
# this function makes this kind of thing tonnes easier
from py2neo.types import remote


class GraphObjectWithRelProps(GraphObject):
    """Simple extension to add get method for a relationship
    between self and another node"""

    @staticmethod
    def _get_relationship(source, target, relationship):
        """Returns the relationship connecting source and target as set
        in the class definition.
        Copy this method, adding an argument for the params
        and altering the cypher to add a set function

        Only works if two classes are rleated by only one relationship

        :param source: GraphObject instance - the starting node
        :param target: GraphObject instance - the ending node
        :param relationship: str name of the relationship on source"""

        # get relationship pattern
        rel = getattr(source, relationship)
        # the pattern is calculated for us on this object
        pattern = rel._RelatedObjects__relationship_pattern

        # basic cypher query
        q = ('MATCH {0} '
             'WHERE id(a) = $sId AND id(b) = $tId '
             'RETURN _').format(pattern)
        # for a set operation you would add 
        # DELETE _ CREATE {0} SET _ += $relParams
        # where $relParams is supplied as a dict with your params
        # you could also add a patch method using _ += $relParams
        # the remote function allows us to get a reference to the graph
        graph = remote(source.__ogm__.node).graph
        # as well as the node IDs of the GraphObject
        params = {
            'sId': remote(source.__ogm__.node)._id,
            'tId': remote(target.__ogm__.node)._id,
        }

        return graph.run(q, params).evaluate()


class Intent(GraphObjectWithRelProps):
    name = Property()
    message = RelatedTo("Message", "HAS")

    def get_message_rel(self, n):
        graph = remote(self.__ogm__.node).graph
        return self._get_relationship(graph, self, n, 'message')


class Message(GraphObjectWithRelProps):
    name = Property()
    owner = RelatedFrom("Intent","HAS")

    def get_owner_rel(self, n):
        graph = remote(self.__ogm__.node).graph
        return self._get_relationship(graph, self, n, 'owner')

谢谢多姆·韦尔登。我要试试这个。还有一个问题,关系将来会被支持为对象吗?因此,如果我要添加我的热处理,py2neo,那么关于py2neo未来的最新更新最好取自本期。如果您熟悉python和/或对象关系映射,OGM是掌握图形的一个极好工具。如果你想认真对待图表并将其用于分析或制作,你需要编写密码。这是一种很棒的语言,而且是开源的,掌握它,你就会离开。使用cypher解决了OGM的一个主要问题(它也适用于ORM),如果你在做一些复杂或数据量大的事情,它会给数据库带来疯狂的负载,因为它不是为这些环境设计的,您无法控制它发送到图形以优化它们的查询。从python的角度来看,这意味着(根据我的经验)混合了大量的cypher和python。我对这个问题的答案是
pip安装oopysql