DEV Community

Cover image for A Definitive Guideline for Creating APIs with Django and Neo4j database, Part-1
Afroza Nowshin
Afroza Nowshin

Posted on • Updated on

A Definitive Guideline for Creating APIs with Django and Neo4j database, Part-1

In this post, we will work on our graph database and setup all the necessary connections for building our APIs. As the name suggests, we will be using Neo4j database for our project. The entire architecture for this project is like this, courtesy to this (only) link on the subject matter:

Architecture

Today, we will tear down the rightmost part of this diagram - Neo4j database and the model. Basically, we are using Django framework to provide the API URLs and what each URL will serve. The steps are as follows:

  1. Download and install Neo4j
  2. Download cypher-shell and update credentials
  3. Setup Remote Database connection
  4. Setup database connection with Django
  5. Manipulate OGM
  6. Migrate the database

1. Download and install Neo4j

Go to the official website of Neo4j and download the desktop application - here, and sign up an account. You will be provided with an activation key while downloading, copy it paste it during installation.

2. Download cypher-shell and update credentials

This is an optional step. Cypher shell provides you with a console where you can write CQL or Cypher Query Language, just like a python shell. You can download this from here - Official Download Link.

The default graph database in free version is neo4j with the password neo4j. You can change the password with the following command:

ALTER USER neo4j SET PASSWORD '<new-password>';

Enter fullscreen mode Exit fullscreen mode

Mark this new password because it is needed for setting up the connection with the database.

3. Setup Remote Database connection

  • Go to Projects:
    1

  • Create a new project with a name :
    2

  • There is a dashboard for your project that looks like this:
    3

  • Click Add and then the marked menu item:
    4

  • Give a DBMS name and set the url like the format shown in the picture, then click Next:
    5

  • Provide your database username and password, then click save:
    6

  • Click Connect to set the connection with the database:
    7

  • Click Open to see in the browser:
    8

  • In the Browser, click the green highlighted db icon:
    9

  • Select neo4j and it will now show the default movie table.

10

4. Setup database connection with Django

First, create a Django project and then create an app inside the project for the APIs.

django-admin startproject mysite
cd mysite/
python manage.py startapp yourapp

Enter fullscreen mode Exit fullscreen mode

Now go to your settings.py and import config from neomodel. This is required to set up our database with Django. Your console should look like this:

django1

Go to your settings.py and the following lines in the respective places:

from neomodel import config

config.DATABASE_URL = 'bolt://username:password@localhost:7687'

# you can either use bolt or neo4j for the connection type but bolt is preferable

Enter fullscreen mode Exit fullscreen mode

Replace the username and the password with yours.

5. Manipulate OGM

From Django's official documentation, "Once you've created your data models, Django automatically gives you a database-abstraction API that lets you create, retrieve, update and delete objects." - this is the ORM or object-relational mapping layer. To break it down in simpler terms, Django models are like a mirror to your database; it models your database tables with its own abstraction layer. Your API requests are passed through this abstraction layer under the hood.

As we are using a graph database, we have nodes instead of tables and edges to capture the relationship. Neo4j provides OGM or object-graph mapping library with Neo4j-OGM. To use this, first import the field types and follow this code:


from neomodel import StructuredNode, StringProperty, IntegerProperty,UniqueIdProperty, RelationshipTo, BooleanProperty, EmailProperty, DateTimeProperty, DateProperty, config

class NeoUser(StructuredNode):
    uid = UniqueIdProperty()
    username = StringProperty()
    email = EmailProperty(unique_index=True)
    password = StringProperty()

class NeoPosts(StructuredNode):
    ....

Enter fullscreen mode Exit fullscreen mode

To design the relationship, You have to write like the following (refer to ERD from the previous blog post):

# Relations :
    post = RelationshipTo(NeoPosts, 'CREATES')
    followers = RelationshipTo('NeoUser','FOLLOWERS')
Enter fullscreen mode Exit fullscreen mode

6. Migrate the database

For Django models, you were supposed to run the following commands.

python manage.py makemigrations
python manage.py migrate

Enter fullscreen mode Exit fullscreen mode

However, since we are making nodes in this model, we need to use the following command:

python manage.py install_labels

Enter fullscreen mode Exit fullscreen mode

Now you can view your created model and the nodes like the following image:

11

That's it for this post. In the next post, we will learn to build the APIs for the model that we have built. I will publish the next part soon. Your feedback is highly appreciated.

Top comments (0)