我正在使用图形对象映射,其中我有2个对象来表示2个节点。我想拥有关系的属性,如何定义与属性关系的类? Here,我看到了一个例子,但没有使用对象。
我的代码如下:
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,是否存在关系的东西?)
答案 0 :(得分:1)
py2neo
的当前版本不允许关联对象',这将允许您以标准OOP-y方式将参数放入关系中。
OGM对于开始项目和快速插入/检索事务非常有用,但对于大多数更复杂的事务,您将需要编写cypher来实现您的目标。
但是,您经常希望在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')