App & Software, Dev & Design, Tech, Tutorials

Django and Neo4J Tutorial

Django and Neo4J Image Courtsey: Flickr (Bryan Veloso)

In this tutorial, We are going to show you how your django app can communicate to your neo4j graph database instance using the py2neo package. For the sake of simplicity and easy understanding of the implementation, we are going to setup a fresh instance of django and neo4j and yeah keep this tutorial brief. Before we get into action let’s first define what we are planning to achieve. So for this tutorial say we have a django app that stores information about “Books” and “Authors” and by the means of using neo4j graph database, we want to show the relationships between authors and books.

Django and Neo4J

So let’s begin and quickly setup our django app. See the video for django app setup.

Installing Neo4J

Now let’s setup neo4j graph database instance. So we are going to download the community addition from the neo4j’s download page. We chose to download the tarball, you can choose what package suits your operating system the best. Once you have the package download simply untar/uncompress it, navigate to the uncompressed folder and launch it using the following command.

once it says the server is ready, Open an internet browser and navigate to http://localhost:7474/ and you should see something like the following:

Django and Neo4J


Alrighty, Now we have both our django and neo4j instance setup. Let’s say we want add an author node to our neo4j database as soon as it is created via our django app. In order to achieve this, we’ll make the use django signals and py2neo package to send queries to our graph database. Let’s try it. We’ll first implement a post save signal for the Author model that calls a dummy method.

Currently our create_author_node method doesn’t do anything. To have it communicate with neo4j server we’ll require py2neo so let’s install it.

Once it is installed, we are going to modify our create_author_node to send a query to neo4j database.

This should create an author node for each author that is now created in your django application. Let’s try it, Go to django shell and now try creating a new author object.

Now navigate to http://localhost:7474/ and run the following query and you should see the author node created with properties first_name and last_name.

django and neo4j

django and neo4j

Similarly, you can send both read/write cypher queries to make django communicate with your neo4j server. The above example is just for the sake of learning, for real world examples please make sure you execute graph queries “asynchronously” using celery or something. If you have any questions or queries, do let me know in the comments below and I will try my best to resolve it.

You Might Also Like

  • aditya mathur

    Amazing blog Amyth , can you please share a example of how to update the Author details ?

    • geekunlimited

      @adityamathur:disqus , Thanks. To update the properties on a node, simply use SET cypher query. For e.g. MATCH (x:Author {first_name:"Chetan"}) SET x.firstname="Aditya", x.last_name="Mathur" RETURN x