django 联系neo4j_从Django访问Neo4j的最佳方式是什么?

本文探讨了使用Django访问Neo4j的两种方式:neo4j-embedded和neo4j-rest-client。尽管neo4j-embedded提供更好的性能,但可能不包含Django集成。而neo4j-rest-client基于REST API,适合远程数据库操作,但速度较慢。作者建议根据性能需求和是否使用远程数据库来选择合适的方式。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

it seems that i found something that let me confused; i've found two Neo4j to download to python, the first one is:

and the second one is:

what's the difference between the two? the first one seems to be big (size), so does this mean that if i use it i'll not need the neo4j community release (milestone)?

when i've installed the first one, and tried to test a django example, it seems that the directory named "model" https://svn.neo4j.org/components/neo4j.py/trunk/src/main/python/neo4j/model/ is missing? so what's the difference, and who will be better to use with Django?

and what about that one?

解决方案

I'm part of the team that began updating neo4django from Tobias's original integration, but I'll try to be fair =)

You skipped another important library- neo4j-rest-client, the Python REST binding. neo4django is based on this library, and shares a bunch of its advantages and disadvantages. In particular, a REST client will always be slower than a native client, but it allows the easy use of a remote database.

neo4django is a pretty tight integration with Django. It's very similar to the standard ORM, and has some cool goodies like queries with index support and working alongside a relational DB. I like it alot, but if you aren't using a remote database, the performance penalty might be too high. I'm working on it, though, so keep posted!

I believe the svn link above (https://svn.neo4j.org/components/neo4j.py/trunk/) is the old native Python binding. The new one is way better from what I've seen (less of a headache, updated), so unless you want to give the old bundled Django integration a try, it'd be easier to avoid it.

The first link (http://pypi.python.org/pypi/neo4j-embedded) is the newer integration. I don't think 'model' is included in the distro, because it doesn't yet come with Django integration. If you need serious performance and you're okay with dropping the Django-style data-definition-language, it's the way to go.

In the future, I hope to get neo4django working with the native and REST clients... we'll see what happens!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Django中使用Neo4j数据库,需要执行以下步骤: 1. 安装py2neo库:这是一个Python库,用于在Python中与Neo4j数据库进行交互。可以使用以下命令安装: ``` pip install py2neo ``` 2. 在Django项目中配置数据库连接:在settings.py文件中,添加以下内容来配置Neo4j数据库连接: ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', }, 'neo4j': { 'ENGINE': 'django_neo4j_engine', 'NAME': 'neo4j', 'HOST': 'localhost', 'PORT': 7687, 'OPTIONS': { 'username': 'neo4j', 'password': 'password', } } } ``` 在这个例子中,我们使用django_neo4j_engine来连接到Neo4j数据库,使用的是默认的用户名和密码。 3. 创建模型:在models.py文件中创建模型,例如: ```python from django_neomodel import DjangoNode class Person(DjangoNode): name = StringProperty(max_length=255) age = IntegerProperty() # ...其他属性和方法 ``` 这个模型继承自DjangoNode,它允许我们在Django中使用Neo4j模型。 4. 执行查询:在views.py文件中,我们可以执行Neo4j查询,例如: ```python from django.http import HttpResponse from myapp.models import Person def myview(request): # 查询所有人物 people = Person.nodes.all() # 构建响应 response = '' for person in people: response += f'{person.name}, {person.age}<br/>' return HttpResponse(response) ``` 这个视查询所有人物,并返回他们的名字和年龄。 5. 启动Django服务器:现在我们可以启动Django服务器并访问这个视了。使用以下命令启动服务器: ``` python manage.py runserver ``` 在浏览器中访问http://localhost:8000/myview,应该能够看到查询结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值