1
我無法找到節點與之存在關係的節點。 我希望能夠從選定節點中找到與所選節點具有關係的節點。py2neo:從關係和所有傳入關係中獲取末端節點而不使用密碼
下面是示例圖表代碼:
sarah = Woman.select(graph, primary_value="Sarah").first()
sarah.likes._related_objects[0][0].name
# returns "John"
# or
list(sarah.__ogm__.related.values())[0]._related_objects[0][0].name
# returns "John"
我無法找到任何方式:
from py2neo import Node, Relationship, Graph, NodeSelector, Path
from py2neo.ogm import *
graph = Graph(user = 'neo4j', password = 'neo4j')
graph.delete_all()
class Man(GraphObject):
__primarykey__ = "name"
name = Property("name")
likes = RelatedTo("Woman", "LIKES")
class Woman(GraphObject):
__primarykey__ = "name"
name = Property("name")
likes = RelatedTo("Man", "LIKES")
new_man = Man()
new_man.name = "John"
graph.push(new_man)
new_woman = Woman()
new_woman.name = "Sarah"
new_woman.likes.add(Man.select(graph, primary_value="John").first())
graph.push(new_woman)
new_man = Man()
new_man.name = "Joe"
new_man.likes.add(Woman.select(graph, primary_value="Sarah").first())
graph.push(new_man)
我在得到的莎拉喜歡誰的名字嘗試在不看其他節點的情況下獲得誰喜歡莎拉的名字。這是可能的還是我浪費我的時間?有沒有更好的方法來做到這一點? 上午我堅持:
def get_who_likes_sarah():
names = []
for m in Man.select(graph):
try:
name = m.likes._related_objects[0][0].name
if name == "Sarah":
names.append(m.name)
except:
pass
return names
你有一個解決方案? –
放棄並留下我所擁有的。我的代碼結束了這樣的事情 '[m.name for m in Man.select(graph)for w in m.likes._related_objects if w [0] .name ==「Sarah」]' – Jimi