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 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 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_ENCRYPTED_CONNECTION = True NEOMODEL_MAX_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 2.7, 3.4+
neo4j 3.0+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_neomodel-0.0.2.tar.gz
.
File metadata
- Download URL: django_neomodel-0.0.2.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcdc82cb19d26f8771bda162603abefa32dafabfd6e5c853f74d015665f0a815 |
|
MD5 | 1e437797b386a234284b00797eb9337c |
|
BLAKE2b-256 | 399c1c1c6534e54221d7cc16b4456a7befd01b49ef60b7957557cbdbc1531f8c |
File details
Details for the file django_neomodel-0.0.2-py2-none-any.whl
.
File metadata
- Download URL: django_neomodel-0.0.2-py2-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d8beba50998a3d8bd17c1e7761d97109eed9e8c288a7b79392ae6cb8106e8d6 |
|
MD5 | 4597933cb478e6398a0c2d57be776671 |
|
BLAKE2b-256 | e6e32bffdc5f46f4cd78f97a1f5f0b3e9324e95eb930d06626085b22f6eb2034 |