可以使用neomodel在django中制作模型吗?如何在 django 中集成 neo4j?我使用的是Python 3,所以neo4django并不是一个真正的选择。我对他们两个都是新手,目前我有点困惑......
非常感谢! :3
嘿 neomodel 开箱即用地支持 python 3,你可以在有或没有 django 的情况下使用它,请查看这里的文档:http://neomodel.readthedocs.org/en/latest/<</p>
NEOMODEL_NEO4J_BOLT_URL = bolt://neo4j:password@localhost:7687
NEOMODEL_SIGNALS = True
NEOMODEL_FORCE_TIMEZONE = False
NEOMODEL_ENCRYPTED_CONNECTION = True
NEOMODEL_MAX_POOL_SIZE = 50
您需要在 settings.py 文件中添加上述行才能使用 Neo4j。
我知道这是 8 年前问过的,但现在有一个名为 neomodel 的工具可以达到这个确切的目的。它也可以与django-neomodel结合使用,这使得neomodel可以很容易地集成到你的django项目中。
使用 django-neomodel,您需要做的就是在settings.py
文件中指定数据库的 URL,如下所示:
NEOMODEL_NEO4J_BOLT_URL = 'bolt://{username}:{password}@{HOSTorIP}'
您可以在models.py
文件中轻松创建模型。以下是此新模型文档的一些示例:
from neomodel import (StructuredNode, StringProperty,
UniqueIdProperty, IntegerProperty,
RelationshipTo)
class Country(StructuredNode):
code = StringProperty(unique_index=True, required=True)
class Person(StructuredNode):
uid = UniqueIdProperty()
name = StringProperty(unique_index=True)
age = IntegerProperty(index=True, default=0)
# traverse outgoing IS_FROM relations, inflate to Country objects
country = RelationshipTo(Country, 'IS_FROM')
然后,您可以运行 python manage.py install_labels
来执行与运行迁移等效的操作,或者python manage.py clear_neo4j
从数据库中清除所有节点。
可以像这样创建节点:
from models import Person
john = Person(name="john", age=23).save()
frank = Person(name="frank", age=50).save()
canada = Country(code=5).save()
以及这样的关系:
john.country.connect(canada)
节点/关系可以按如下方式检索:
frank = Person.nodes.get(name='frank')
frank.age += 1
frank.save()
franks_country = frank.country
print(franks_country)
# {'code': 5}