Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create REST API with Neo4J and Django

I am trying to create a REST API with Neo4j and Django in the backend.

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.

Thanks for you help


EDIT: 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"}

Here is my code :

api.py file:

class NodeResource (ModelResource): #it doesn't work with Resource neither
    class meta:
        queryset= Node.objects.all()
        resource_name = 'node'

urls.py file:

node_resource= NodeResource()

urlpatterns = patterns('',
    url(r'^api/', include(node_resource.urls)),

models.py file :

class Node(models.NodeModel):
    p1 = models.StringProperty()
    p2 = models.StringProperty()
like image 933
Anas Avatar asked Jun 04 '13 14:06

Anas


3 Answers

I would advise steering away from passing Neo4j REST API responses directly through your application. Not only would you not be in control of the structure of these data formats as they evolve and deprecate (which they do) but you would be exposing unnecessary internals of your database layer.

Besides Neo4Django, you have a couple of other options you might want to consider. Neomodel is another model layer designed for Django and intended to act like the built-in ORM; you also have the option of the raw OGM layer provided by py2neo which may help but isn't Django-specific.

It's worth remembering that Django and its plug-ins have been designed around a traditional RDBMS, not a graph database, so none of these solutions will be perfect. Whatever you choose, you're likely to have to carry out a fair amount of transformation work to create your application's API.

like image 86
Nigel Small Avatar answered Sep 20 '22 13:09

Nigel Small


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.

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.

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 :)

like image 22
Anas Avatar answered Sep 21 '22 13:09

Anas


Thanks to recent contributions, Neo4django now supports Tastypie out of the box! I'd love to know what you think if you try it out.

EDIT:

I've just run through the tastypie tutorial, and posted a gist with the resulting example. I noticed nested resources are a little funny, but otherwise it works great. I'm pretty sure the gents who contributed the patches enabling this support also know how to take care of nested resources- I'll ask them to speak up.

EDIT:

As long as relationships are specified in the ModelResource, they work great. If anyone would like to see examples, let me know.

like image 33
Matt Luongo Avatar answered Sep 17 '22 13:09

Matt Luongo