Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Use Neo4j with Django!

Project description

Django Neomodel (beta!)


This module allows you to use the neo4j graph database with Django using neomodel

Getting started

Install the module:

$ pip install django_neomodel

Add the following settings to your

NEOMODEL_NEO4J_BOLT_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://neo4j:test@localhost:7687')

# Make sure django_neomodel comes before your own apps
    # django.contrib.auth etc

Write your first node definition in yourapp/

from neomodel import StructuredNode, StringProperty, DateProperty

class Book(StructuredNode):
    title = StringProperty(unique_index=True)
    published = DateProperty()

Create any constraints or indexes for your labels. This needs to be done after you change your node definitions much like migrate:

$ python install_labels

Now in a view yourapp/

from .models import Book

def get_books(request):
    return render('yourapp/books.html', request, {'books': Book.nodes.all()})

And you’re ready to go. Don’t forget to check the neomodel documentation.

Model forms

Switch the base class from StructuredNode to DjangoNode and add a ‘Meta’ class:

from datetime import datetime
from django_neomodel import DjangoNode
from neomodel import StructuredNode, StringProperty, DateTimeProperty, UniqueIdProperty

class Book(DjangoNode):
    uid = UniqueIdProperty()
    title = StringProperty(unique_index=True)
    status = StringProperty(choices=(
            ('Available', 'A'),
            ('On loan', 'L'),
            ('Damaged', 'D'),
        ), default='Available')
    created = DateTimeProperty(default=datetime.utcnow)

    class Meta:
        app_label = 'library'

Create a model form class for your DjangoNode:

class BookForm(ModelForm):
    class Meta:
        model = Book
        fields = ['title', 'status']

This class may now be used just like any other Django form.


The following config options are available in django settings (default values shown). These are mapped to neomodel.config as django is started:

NEOMODEL_NEO4J_BOLT_URL = 'bolt://neo4j:neo4j@localhost:7687'


Signals work with DjangoNode sub-classes:

from django.db.models import signals
from django_neomodel import DjangoNode
from neomodel import StringProperty

class Book(DjangoNode):
  title = StringProperty(unique_index=True)

def your_signal_func(sender, instance, signal, created):

signals.post_save.connect(your_signal_func, sender=Book)

The following are supported: pre_save, post_save, pre_delete, post_delete. On freshly created nodes created=True in the post_save signal argument.


You can create a setup method which clears the database before executing each test:

from neomodel import db, clear_neo4j_database

class YourTestClass(DjangoTestCase):
    def setUp(self):

    def test_something(self):

Management Commands

The following django management commands have been included.


Setup constraints and indexes on labels for your node definitions. This should be executed after any schema changes:

$ python install_labels
Setting up labels and constraints...

Found tests.someapp.models.Book
+ Creating unique constraint for title on label Book for class tests.someapp.models.Book

Finished 1 class(es).


Delete all nodes in your database, warning there is no confirmation!


  • Python 2.7, 3.4+
  • neo4j 3.0+
Join the chat at

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django_neomodel, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size django_neomodel-0.0.4-py2-none-any.whl (15.0 kB) File type Wheel Python version 2.7 Upload date Hashes View hashes
Filename, size django_neomodel-0.0.4.tar.gz (9.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page