使用 Neo4J 和 Django 创建 REST API [英] Create REST API with Neo4J and Django
问题描述
我正在尝试在后端使用 Neo4j 和 Django 创建一个 REST API.
I am trying to create a REST API with Neo4j and Django in the backend.
问题是,即使我有使用 Neo4Django 的 Django 模型,我也不能使用像 Tastypie 或 Piston 这样通常将模型序列化为 JSON(或 XML)的框架.
The problem is that even when I have Django models using Neo4Django , I can't use frameworks like Tastypie or Piston that normally serialize models into JSON (or XML).
抱歉,如果我的问题令人困惑或不清楚,我是网络服务的新手.
Sorry if my question is confusing or not clear, I am newbie to webservices.
谢谢你的帮助
所以我从 Tastypie 开始,并按照此页面上的教程http://django-tastypie.readthedocs.org/en/latest/tutorial.html
.我正在寻找在浏览器中显示 Neo4j JSON 响应,但是当我尝试访问 http://127.0.0.1:8000/api/node/?format=json
时,我收到此错误:
So I started with Tastypie and followed the tutorial on this page http://django-tastypie.readthedocs.org/en/latest/tutorial.html
. I am looking for displaying the Neo4j JSON response in the browser, but when I try to access to http://127.0.0.1:8000/api/node/?format=json
I get this error instead:
{"error_message": "'NoneType' object is not callable", "traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.6/dist-packages/tastypie/resources.py\", line 217, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/usr/local/lib/python2.6/dist-packages/tastypie/resources.py\", line 459, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/usr/local/lib/python2.6/dist-packages/tastypie/resources.py\", line 491, in dispatch\n response = method(request, **kwargs)\n\n File \"/usr/local/lib/python2.6/dist-packages/tastypie/resources.py\", line 1298, in get_list\n base_bundle = self.build_bundle(request=request)\n\n File \"/usr/local/lib/python2.6/dist-packages/tastypie/resources.py\", line 718, in build_bundle\n obj = self._meta.object_class()\n\nTypeError: 'NoneType' object is not callable\n"}
这是我的代码:
api.py 文件:
class NodeResource (ModelResource): #it doesn't work with Resource neither
class meta:
queryset= Node.objects.all()
resource_name = 'node'
urls.py 文件:
urls.py file:
node_resource= NodeResource()
urlpatterns = patterns('',
url(r'^api/', include(node_resource.urls)),
models.py 文件:
models.py file :
class Node(models.NodeModel):
p1 = models.StringProperty()
p2 = models.StringProperty()
推荐答案
Django-Tastypie 允许使用 NoSQL 数据库创建 REST API,以及在 http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html.
Django-Tastypie allows to create REST APIs with NoSQL databases as well as mentioned in http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html.
原则是使用tastypie.resources.Resource
而不是tastypie.resources.ModelResource
,后者是RDBMS特有的,那么必须重新定义main函数才能提供带有所需参数的 JSON.
The principle is to use tastypie.resources.Resource
and not tastypie.resources.ModelResource
which is SPECIFIC to RDBMS, then main functions must be redefined in order to provide a JSON with the desired parameters.
因此,我采用了链接中给出的示例,对其进行了修改,并使用 Python 的 Neo4j REST 客户端来获取数据库的实例并执行请求,它的工作非常出色.
So I took the example given in the link, modified it and used Neo4j REST Client for Python to get an instance of the db and perform requests, and it worked like a charm.
感谢您的所有回复:)
Thanks for all your responses :)
这篇关于使用 Neo4J 和 Django 创建 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!