Register Your Neo4j-Based Models to Django Admin

A Paradise Papers Example

Cristina
Neo4j Developer Blog
4 min readJul 9, 2021

--

If you’re a Django developer, you may have already found the popular py2neo toolkit and may have some experience using Neomodel and/or the Neo4j python driver directly. But how well do these tools play with Django?

Thankfully for Django developers, the Neo4j ecosystem provides a quick way to spin up an example app — the Paradise Papers Search App — based on the Paradise Papers dataset, conveniently available on Neo4j Sandbox.

This post will be a quick walkthrough of how to register a model on the Paradise Papers Search app. For more details, check out our previous post on the topic here:

Local Setup — Sandbox Database

First step, set up your local environment:

clone the repo:

git clone git@github.com:neo4j-examples/paradise-papers-django.git

In a virtual environment, install the requirements:

pip install -r requirements.txt

Next you’ll need to point your app to a sandbox instance of the database.

In Neo4j Sandbox, create an account and select Paradise Papers by ICIJ.

Back in the Sandbox UI, tap “Connection details” to find the database’s Bolt URL, username, and password.

In your local environment, set the DATABASE_URL variable using the credentials found in the Sandbox admin:

export DATABASE_URL=bolt://<Username>:<Password>@<IP Address>:7687

In paradise_papers_search/fetch_api/admin, add the models you would like to explore using the admin:

from django.contrib import admin as dj_admin
from django_neomodel import admin as neo_admin
from .models import Entityclass EntityAdmin(dj_admin.ModelAdmin):
list_display = (“name”,)
neo_admin.register(Entity, EntityAdmin)

Add the admin URL to urls.py (if you haven’t done so already):

from django.conf.urls import url, include
from django.views.generic import TemplateView
from django.contrib import admin
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
url(r'^fetch/', include('fetch_api.urls')),
url(r"^admin/", admin.site.urls)
]

Because Django’s admin authentication still goes through Django models, you will you need to set up a (relational) user database in your settings file and run the migrations.

Example settings snippet:

DATABASES = {
'default': {
'NAME': 'papers.db',
'ENGINE': 'django.db.backends.sqlite3',
'USER': '',
'PASSWORD': '',
'PORT': '',
},
}

Running the migrations:

./manage.py migrate

After that, create the admin superuser:

./manage.py createsuperuser

Run the app!

python manage.py runserver — settings=paradise_papers_search.settings.dev

Start searching at http://127.0.0.1:8000/

Log in to the admin at http://127.0.0.1:8000/admin

--

--