• Stars
    star
    286
  • Rank 144,690 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Neomodel plugin for Django

Django Neomodel (beta!)

neomodel

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

Warnings

Live Examples (add yours here)

Getting started

Install the module:

$ pip install django_neomodel

Add the following settings to your settings.py:

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

# Make sure django_neomodel comes before your own apps
INSTALLED_APPS = (
    # django.contrib.auth etc
    'django_neomodel',
    'yourapp'
)

Write your first node definition in yourapp/models.py:

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 manage.py migrate:

$ python manage.py install_labels

Now in a view yourapp/views.py:

from .models import Book

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

In your yourapp/admin.py:

from django_neomodel import admin as neo_admin
from .models import Book

class BookAdmin(dj_admin.ModelAdmin):
    list_display = ("title", "created")
neo_admin.register(Book, BookAdmin)

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.

Settings

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'
NEOMODEL_SIGNALS = True
NEOMODEL_FORCE_TIMEZONE = False
NEOMODEL_MAX_CONNECTION_POOL_SIZE = 50

Signals

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):
    pass

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.

Testing

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):
        clear_neo4j_database(db)

    def test_something(self):
        pass

Management Commands

The following django management commands have been included.

install_labels

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

$ python manage.py 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).

clear_neo4j

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

Requirements

  • Python 3.7+
  • neo4j 5.x, 4.4 (LTS)
Join the chat at https://gitter.im/robinedwards/neomodel

Docker Example

Using Docker Compose.

Commands to setup Docker Container docker-entrypoint.sh:

# Go to tests
$ cd tests/
# Docker Command (Make sure Docker is running and up to date)
$ docker-compose up
# login in admin with username=admin password=1234

Go to http://localhost:7474/browser/

Go to http://localhost:8000/admin/

Running Tests

Setup Neo4j Desktop with a local database with password 'foobarbaz' and version 5.x or 4.4.x (Neo4j LTS version).

Commands to run tests:

# create local venv and install dependencies.
$ pip install -e '.[dev]'; export DJANGO_SETTINGS_MODULE=tests.settings;
$ tests/manage.py install_labels
$ tests/manage.py migrate
$ pytest

# example output:

platform darwin -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
pick 0900469 Neo4J-update-t-4.1
collected 16 items

someapp/tests/test_atomicity.py .                                                                                                                                                                                                                      [  6%]
someapp/tests/test_commands.py ..                                                                                                                                                                                                                      [ 18%]
someapp/tests/test_model_form.py ...........                                                                                                                                                                                                           [ 87%]
someapp/tests/test_sanity.py .                                                                                                                                                                                                                         [ 93%]
someapp/tests/test_signals.py .
16 passed, 11 warnings in 1.62s

More Repositories

1

neo4j-apoc-procedures

Awesome Procedures On Cypher for Neo4j - codenamed "apoc"                     If you like it, please ★ above ⇧            
Java
1,706
star
2

neovis.js

Neo4j + vis.js = neovis.js. Graph visualizations in the browser with data from Neo4j.
TypeScript
1,601
star
3

neomodel

An Object Graph Mapper (OGM) for the Neo4j graph database.
Python
955
star
4

spatial

Neo4j Spatial is a library of utilities for Neo4j that faciliates the enabling of spatial operations on data. In particular you can add spatial indexes to already located data, and perform spatial operations on the data like searching for data within specified regions or within a specified distance of a point of interest. In addition classes are provided to expose the data to geotools and thereby to geotools enabled applications like geoserver and uDig.
Scheme
780
star
5

neo4j-graph-algorithms

Efficient Graph Algorithms for Neo4j
Java
771
star
6

neo4j-mazerunner

Mazerunner extends a Neo4j graph database to run scheduled big data graph compute algorithms at scale with HDFS and Apache Spark.
Java
381
star
7

neoclipse

Graph Database Tool
Java
219
star
8

neo4j-etl

Data import from relational databases to Neo4j.
HTML
215
star
9

neo4j-elasticsearch

Neo4j ElasticSearch Integration
Java
211
star
10

graphgist

Easy publishing with graph data included
JavaScript
208
star
11

neo4j-streams

Neo4j Kafka Connector
Kotlin
173
star
12

neo4j-helm

Helm Charts for running Neo4j on Kubernetes [DEPRECATED]
Shell
88
star
13

rabbithole

Interactive, embeddable Neo4j-Console
Java
79
star
14

neo4j-mobile-android

Neo4j for Android
Java
78
star
15

authentication-extension

Neo4j Server Auth Extension
Java
72
star
16

gists

Gists for use in GraphGists.
68
star
17

sparql-plugin

Java
67
star
18

kubernetes-neo4j

(RETIRED) Kubernetes experiments with Neo4j. See updated Helm Repo
60
star
19

neo4j-tableau

Neo4j Tableau Integration via WDC
Java
59
star
20

gremlin-plugin

A Plugin for the Neo4j server add Tinkerpop-related functionality
Java
55
star
21

neo4j-org

neo4j.org website
JavaScript
55
star
22

ec2neo

CloudFormation Templates for deploying Neo4j
Ruby
51
star
23

graph-collections

In-graph collections for the Neo4j graph database.
Java
49
star
24

twitter-neo4j

CSS
46
star
25

osm

OSM Data Model for Neo4j
Java
46
star
26

trumpworld-graph

Import, Extend, Query & Visualize the TrumpWorld Graph with Neo4j & Cypher (originally based on the BuzzFeed data)
Jupyter Notebook
43
star
27

fast-http

Fast HTTP protocol with separate netty+disruptor based server
Java
42
star
28

neo4j-meetups-reporting

This is an end-to-end graph-based reporting sample built on Neo4j for tracking and measuring meetup group membership statistics over time.
JavaScript
38
star
29

neo4j-ml-procedures

This project provides procedures and functions to support machine learning applications with Neo4j.
Java
37
star
30

training

HTML
36
star
31

neo4j-faker

Use faker cypher functions to generate demo and test data with cypher
Java
34
star
32

neo4j-guides

Tooling to create Neo4j Browser Guides from AsciiDoc Content
HTML
33
star
33

neo4j-puppet

Puppet module for installing Neo4j on Linux systems
Puppet
30
star
34

sql2cypher

Experimental SQL to Cypher Transpiler using jooq and cypher-dsl
Java
26
star
35

ImageToGraph

A CLI tool to convert images into graphs
Java
25
star
36

python-embedded

Python bindings for Neo4j
Python
25
star
37

neo4j-tinkerpop-api-impl

Implementation of Apache Licensed Neo4j API for Tinkerpop3
Java
24
star
38

training-v3

(new) Neo4j Training Material
Jupyter Notebook
21
star
39

neo4j-csv-import-web

Prototype web app to automate data import of csv files into Neo4j
HTML
21
star
40

spatial-algorithms

Spatial algorithms for both cartesian and geographic data
Java
21
star
41

northwind-neo4j

21
star
42

py2neo

EOL! Py2neo is a comprehensive Neo4j driver library and toolkit for Python.
Python
20
star
43

neo4j-geoff

GEOFF file loader plugin for Neo4j
Java
19
star
44

neo4j-script-procedures

Neo4j Procedures to declare, store and run Javascript, Python, R, Ruby based procedures
Java
18
star
45

training-v2

HTML
14
star
46

sandbox-guides

Resources for building and deploying Neo4j Browser Guides including with sandbox instances
HTML
12
star
47

neo4j-osgi

Java
12
star
48

asciidoc-slides

HTML
11
star
49

javascript-plugin

JavaScript plugin for Neo4j Server.
Java
11
star
50

neo4j-dwh-connector

Scala
11
star
51

neo4j-temp-db

JavaScript
9
star
52

manual-chinese

Shell
9
star
53

neo4j-cassandra-connector

Python command line application for inserting data from Cassandra into Neo4j
Python
8
star
54

m2

Maven Repository
8
star
55

neo4j-jfr

Toolbox to help monitor Neo4j internals with Java Flight Recorder
Java
7
star
56

graph-refactoring

Procedures for Graph Refactorings
Java
7
star
57

datascience-ml-training

Python
6
star
58

developer-resources-fr

French Translation Developer Resources
Shell
6
star
59

script-extension

Server Extension for JVM scripting languages
Java
6
star
60

neo4j-tinkerpop-api

Apache Licensed Neo4j API for Tinkerpop3
Java
6
star
61

neo4j-alexa-skills

Amazon Echo Alexa Skills for querying Neo4j
PHP
6
star
62

cypher-http-examples

Examples on how to connect to the Cypher endpoints from Java with different http libraries
Java
6
star
63

waza-zen-table

Zen-Table Hacking for http://waza.heroku.com
JavaScript
5
star
64

graphgist-portal-v3

GraphGist Portal v3 (JavaScript)
JavaScript
5
star
65

node-neo4j-demo

Template application for Neo4j using Node.js
JavaScript
5
star
66

geoff-plugin

Java
4
star
67

relate-at-graphconnect

Relate! at Graph Connect
4
star
68

neo4j-rdf-sparql

Java
4
star
69

neo4j-rdf

Java
4
star
70

manual-french

Shell
4
star
71

neo_lock_down

Security Rule for Neo4j that disables Traversal REST API
Java
4
star
72

neo4splunk

Neo4Splunk is a Neo4j Splunk Connector using Spunks Python APIs and the Neo4j Python Driver (Proof of Concept)
Python
3
star
73

neo4j-lucene5-index

Neo4j Lucene 5 Integration
Java
3
star
74

local-dataflow-runner

Local Dataflow Runner for the googlecloud-to-neo4j template
Java
3
star
75

neo4j-sproc-compiler

Compile-time annotation processor to verify Neo4j procedure|functions validity
Java
3
star
76

neo4j-community-api

API for Neo4j Community Management
Python
3
star
77

neo4j-data-science-yelp

Online Data Science Class with the Yelp Dataset
Jupyter Notebook
3
star
78

neo4j-contrib.github.io

Neo4j Contrib Landing Page
HTML
2
star
79

design-patterns

2
star
80

community

Neo4j Community Working Group
2
star
81

neo4j-com-examples

Example code for the website
Java
2
star
82

neo4j-utils

Java
2
star
83

training-backend

Neo4j Training Backend for Online Course with Versal
Java
2
star
84

neo4j-library-resources

A resource for neo4j driver/library creators to help creating a helpful website
2
star
85

neo4j-rdf-sail

Java
2
star
86

neo4j-learn

JavaScript
2
star
87

neo4j-dcos

Mesopshere Universe Package(s) for Neo4j Cluster
Shell
2
star
88

neo4j-http

PoC for an external HTTP API using Bolt.
Java
2
star
89

j2ee

Java
2
star
90

neo4j-website

Neo4j Website
JavaScript
2
star
91

neo4j-graph-matching

Java
2
star
92

neo4j-meta-model

Java
2
star
93

legacy-index

Legacy index implementation for Neo4j, i.e. IndexService and LuceneIndexService
Java
2
star
94

sandbox-code-updater

Code Updater for the sandbox repositories in github.com/neo4j-graph-examples
Java
2
star
95

classmarker-integration

HTML
1
star
96

applied-graph-algorithms-training

HTML
1
star
97

neo4j-movies

Basic Graph Domain Model for Movies and Import/Export/Examples/Integration
1
star
98

GSoC

Neo4j Google Summer of Code Projects
1
star
99

aura-professional-on-GCP

Instructions for getting started with Aura
JavaScript
1
star
100

tooling

Java
1
star