A Definitive Guideline for Creating APIs with Django and Neo4j database, Part-1
Afroza Nowshin
Posted on August 24, 2023
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:
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:
- Download and install Neo4j
- Download cypher-shell and update credentials
- Setup Remote Database connection
- Setup database connection with Django
- Manipulate OGM
- 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>';
Mark this new password because it is needed for setting up the connection with the database.
3. Setup Remote Database connection
Give a DBMS name and set the url like the format shown in the picture, then click Next:
Provide your database username and password, then click save:
Select neo4j and it will now show the default movie table.
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
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:
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
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):
....
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')
6. Migrate the database
For Django models, you were supposed to run the following commands.
python manage.py makemigrations
python manage.py migrate
However, since we are making nodes in this model, we need to use the following command:
python manage.py install_labels
Now you can view your created model and the nodes like the following image:
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.
Posted on August 24, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 26, 2023
August 24, 2023