Py2neo (V4) - AttributeError: 'Graph' object has no attribute 'find_one'

Py2neo (V4) - AttributeError: 'Graph' object has no attribute 'find_one'

我正在尝试将 neo4j-flask 应用程序更新为 Py2Neo V4,但我找不到如何替换 "find_one" 函数。 (Nicole White 使用的是 Py2Neo V2)

我的设置:

Requirements.txt(其余代码来自 Nicole White 的 github 存储库):

atomicwrites==1.2.0
attrs==18.1.0
backcall==0.1.0
bcrypt==3.1.4
certifi==2018.8.24
cffi==1.11.5
click==6.7
colorama==0.3.9
decorator==4.3.0
Flask==1.0.2
ipykernel==4.8.2
ipython==6.5.0
ipython-genutils==0.2.0
itsdangerous==0.24
jedi==0.12.1
Jinja2==2.10
jupyter-client==5.2.3
jupyter-console==5.2.0
jupyter-core==4.4.0
MarkupSafe==1.0
more-itertools==4.3.0
neo4j-driver==1.6.1
neotime==1.0.0
parso==0.3.1
passlib==1.7.1
pexpect==4.6.0
pickleshare==0.7.4
pkg-resources==0.0.0
pluggy==0.7.1
prompt-toolkit==1.0.15
ptyprocess==0.6.0
py==1.6.0
py2neo==4.1.0
pycparser==2.18
Pygments==2.2.0
pytest==3.7.3
python-dateutil==2.7.3
pytz==2018.5
pyzmq==17.1.2
simplegeneric==0.8.1
six==1.11.0
tornado==5.1
traitlets==4.3.2
urllib3==1.22
wcwidth==0.1.7
Werkzeug==0.14.1

注册用户时收到的错误:

AttributeError: 'Graph' object has no attribute 'find_one'

"The User.find() method uses py2neo’s Graph.find_one() method to find a node in the database with label :User and the given username, returning a py2neo.Node object. "

在 Py2Neo V3 中,函数 find_one -> https://py2neo.org/v3/database.html?highlight=find#py2neo.database.Graph.find_one 可用。

在 Py2Neo V4 中 https://py2neo.org/v4/matching.html 不再有查找功能。

有人知道如何在 V4 中解决这个问题,或者在此处降级?

py2neo v4 有一个 first 函数可以与 NodeMatcher 一起使用。参见:https://py2neo.org/v4/matching.html#py2neo.matching.NodeMatch.first

就是说...v4 引入了 GraphObjects,我发现它(至少到目前为止)非常简洁。

在链接的 github 示例中,用户创建时使用:

user = Node('User', username=self.username, password=bcrypt.encrypt(password))
graph.create(user)

并找到

user = graph.find_one('User', 'username', self.username)

在 py2neo v4 中我会用

class User(GraphObject):
    __primarykey__ = "username"

    username = Property()
    password = Property()

 lukas = User()
 lukas.username = "lukasott"
 lukas.password = bcrypt.encrypt('somepassword')
 graph.push(lukas)

user = User.match(graph, "lukasott").first()

first 函数,据我所知,提供与 find_one 相同的保证,如引用自 v3 文档 "and does not fail if more than one matching node is found."

的基础上,这里有一个最小的示例,显示了 self.match().first() 而非 find_one() 的用法。 属性设置为 Property() 以提供对底层节点 属性 的访问器。 (此处的文档:https://py2neo.org/v4/ogm.html#py2neo.ogm.Property

from py2neo import Graph, Node
from passlib.hash import bcrypt

from py2neo.ogm import GraphObject, Property

graph = Graph()


class User(GraphObject):
    __primarykey__ = 'username'

    username = Property()
    password = Property()

    def __init__(self, username):
        self.username = username

    def find(self):
        user = self.match(graph, self.username).first()
        return user

    def register(self, password):
        if not self.find():
            user = Node('User', username=self.username, password=bcrypt.encrypt(password))
            graph.create(user)
            return True
        else:
            return False

这对我有用。参考下面的 link

def find(self):
        user = graph.nodes.match("User", self.username).first()
        return user

https://py2neo.org/v5/_modules/py2neo/database.html

另一种更简单的解决方法是将 find_one 替换为以下内容:

from py2neo import Graph, NodeMatcher
matcher = NodeMatcher(graph)

user = matcher.match('user', name='name').first()