Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对Neomodel使用多个标签_Python_Neo4j_Neomodel - Fatal编程技术网

Python 对Neomodel使用多个标签

Python 对Neomodel使用多个标签,python,neo4j,neomodel,Python,Neo4j,Neomodel,我想知道是否有一种方法可以将不同的标签与NeoModel关联到一个类。如果没有,什么模块可以允许我这样做 我的理解是,当使用下面的类声明时,“Person”是一个标签 class Person(StructuredNode): name = StringProperty(unique_index=True) age = IntegerProperty(index=True, default=0) 假设我想添加第二个标签,“就业”、“失业”、“学生” 有了Cypher,我可以使用

我想知道是否有一种方法可以将不同的标签与NeoModel关联到一个类。如果没有,什么模块可以允许我这样做

我的理解是,当使用下面的类声明时,“Person”是一个标签

class Person(StructuredNode):
    name = StringProperty(unique_index=True)
    age = IntegerProperty(index=True, default=0)
假设我想添加第二个标签,“就业”、“失业”、“学生”

有了Cypher,我可以使用:
CREATE(p:Person:Student)

我是否可以通过NeoModel实现同样的效果

注:
根据我的研究,使用标签比使用属性(neo4j/cypher)产生更快的查询,这就是为什么我希望就业/失业/学生成为标签的原因。否则,我可以将“occulation”添加为节点属性。

目前没有将标签添加到neomodel structurednode的方法,但是这可以通过cypher完成。我很乐意添加一个方法来实现这一点。您必须小心,标签不会与类名冲突。您可以通过节点的labels()方法返回节点的标签

您只需点击
\uuuuu label\uuuuu
属性

class Person(StructuredNode):
    __label__ = 'Label1:Label2:Label3'
    name = StringProperty(unique_index=True)
    age = IntegerProperty(index=True, default=0)

保存时,它会将标签
Label1
Label2
Label3
添加到创建的节点中

可以使用子类完成,例如:

class Person(StructuredNode):
    name = StringProperty(unique_index=True)
    age = IntegerProperty(index=True, default=0)

class PersonSub(Person):
    pass
然后,实例化子类和call labels()方法:

您还可以使用密码查询来验证:

psub.cypher("MATCH (a:PersonSub) RETURN a")

截至2020年,虽然使用了
neomodel 4.0.1
answer by,但并未产生预期结果。因此,来自的答案是最正确的,但并非没有一个小警告,所以让我详细说明


标签黑客(不推荐!) 首先,让我们看看为什么标签黑客有缺陷:

class Unemployed(StructuredNode):
    __label__ = 'Person:Unemployed'
    name = StringProperty(unique_index=True)


Unemployed(name='Carol').save()
如果选中,则即使节点正确保存在数据库中,也无法在以后正确检测节点:

print(len(Unemployed.nodes))        # prints 0

有人可能会认为,如果我们有另一个类
Person
,那么我们就可以通过这种方式检索它——不幸的是,没有。你自己看看:

class Unemployed(StructuredNode):
    __label__ = 'Person:Unemployed'
    name = StringProperty(unique_index=True)


class Person(StructuredNode):
    name = StringProperty(unique_index=True)


Unemployed(name='Carol').save()
到目前为止,很好,让我们尝试获取一些节点。以下结果看起来不错

print(len(Person.nodes))  # prints 1
但是,当我们尝试访问该节点时,会出现问题:

print(Person.nodes[0])

# Results in two exceptions
#
# Traceback (most recent call last):
# ...
# KeyError: frozenset({'Person', 'Unemployed'})
#
# During handling of the above exception, another exception occurred:
# ...
# neomodel.exceptions.ModelDefinitionMismatch: <exception str() failed>
结果:

print(len(Person.nodes))            # 4
print(len(Student.nodes))           # 2
print(len(Employed.nodes))          # 2
print(len(EmployedStudent.nodes))   # 1

这具有正确的行为,但似乎产生了一个人工制品-标签
EmployeedStudent
。没有简单的黑客可以摆脱这个额外的标签,因为它是至关重要的



结论:OGM有它的缺点,但我会随时选择额外的冗余标签,而不是为我构造的每个类编写密码查询。

因此我切换到py2neo,但py2neo、bulbflow和neomodel之间的详细比较将成为一篇有用的博客文章:)这个答案不再相关。这是可能的通过继承。这对我不起作用。当实现名称字段的唯一约束时,它抱怨密码语法。
class Person(StructuredNode):
    name = StringProperty(unique_index=True)


class Student(Person):
    pass


class Employed(Person):
    pass


class EmployedStudent(Student, Employed):
    pass


Person(name='Bob').save()
Student(name='Will').save()
Employed(name='John').save()
EmployedStudent(name='Kim').save()
print(len(Person.nodes))            # 4
print(len(Student.nodes))           # 2
print(len(Employed.nodes))          # 2
print(len(EmployedStudent.nodes))   # 1