Skip to main content

An object mapper for the neo4j graph database.

Project description

Django Neomodel (beta!)

neomodel

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

https://secure.travis-ci.org/robinedwards/django-neomodel.png

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:test@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()})

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 settings are available with default value shown:

NEOMODEL_NEO4J_BOLT_URL = 'bolt://neo4j:neo4j@localhost:7687'
NEOMODEL_SIGNALS = True
NEOMODEL_FORCE_TIMEZONE = False

Signals

Signals work as expected with StructuredNode sub-classes:

from django.db.models import signals
from neomodel import StructuredNode, StringProperty

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

signals.post_save.connect(your_func, sender=Library)

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 to match your node definitions. This should be executed after any schema changes:

$ python manage.py install_labels
Setting up labels and constraints...

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

clear_neo4j

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

Requirements

  • Python 2.7, 3.4+

  • neo4j 3.0+

Join the chat at https://gitter.im/robinedwards/neomodel

Project details


Download files

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

Source Distribution

django_neomodel-0.0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

django_neomodel-0.0.1-py2-none-any.whl (14.5 kB view details)

Uploaded Python 2

File details

Details for the file django_neomodel-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django_neomodel-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f519886f5e4e5337becf760583f13e7c92707e9d58bbc58b78a53b451a6296d4
MD5 0182008fdf18514817936b7c210d9850
BLAKE2b-256 fc32d1053a9d9276e8cd720a22309491195fb2655e3f77c1d9638cb2af2ffdec

See more details on using hashes here.

File details

Details for the file django_neomodel-0.0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for django_neomodel-0.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 c74daca10f7494b907fc518a3b2c171fdc5513b903ae97175d5376a732b46495
MD5 9020ae65e246091e2f5a91ab5ee1e212
BLAKE2b-256 39920e7a0d712b7e4b1c2a466a10466b2f5bfcad072715af6a1b1ef6eb87e6e0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page