django-neomodel


Namedjango-neomodel JSON
Version 0.1.1 PyPI version JSON
download
home_page
SummaryUse Neo4j with Django!
upload_time2023-08-21 12:46:49
maintainerCristina Escalante
docs_urlNone
author
requires_python>=3.7
licenseMIT
keywords graph neo4j django plugin neomodel
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Django Neomodel (beta!)
=======================

.. image:: https://raw.githubusercontent.com/robinedwards/neomodel/master/doc/source/_static/neomodel-300.png
   :alt: neomodel

This module allows you to use the neo4j_ graph database with Django using neomodel_

.. _neo4j: https://www.neo4j.org
.. _neomodel: http://neomodel.readthedocs.org

Warnings
=======================

* Admin functionality is very experimental. `Please see todos / issues here <https://github.com/neo4j-contrib/django-neomodel/projects/1>`_

Live Examples (add yours here)
===============================

* `ResoTrack <https://resotrack.herokuapp.com/>`_

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)

.. image:: https://badges.gitter.im/Join%20Chat.svg
   :alt: Join the chat at https://gitter.im/robinedwards/neomodel
   :target: https://gitter.im/robinedwards/neomodel?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge

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


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "django-neomodel",
    "maintainer": "Cristina Escalante",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Athanasios Anastasiou <athanastasiou@gmail.com>, Marius Conjeaud <marius.conjeaud@outlook.com>",
    "keywords": "graph,neo4j,django,plugin,neomodel",
    "author": "",
    "author_email": "Robin Edwards <robin.ge@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/54/b5/f20236d02a65421f597cb20808b906ae48765f9d45613024d78b437bceea/django_neomodel-0.1.1.tar.gz",
    "platform": null,
    "description": "Django Neomodel (beta!)\n=======================\n\n.. image:: https://raw.githubusercontent.com/robinedwards/neomodel/master/doc/source/_static/neomodel-300.png\n   :alt: neomodel\n\nThis module allows you to use the neo4j_ graph database with Django using neomodel_\n\n.. _neo4j: https://www.neo4j.org\n.. _neomodel: http://neomodel.readthedocs.org\n\nWarnings\n=======================\n\n* Admin functionality is very experimental. `Please see todos / issues here <https://github.com/neo4j-contrib/django-neomodel/projects/1>`_\n\nLive Examples (add yours here)\n===============================\n\n* `ResoTrack <https://resotrack.herokuapp.com/>`_\n\nGetting started\n===============\n\nInstall the module::\n\n    $ pip install django_neomodel\n\nAdd the following settings to your `settings.py`::\n\n    NEOMODEL_NEO4J_BOLT_URL = os.environ.get('NEO4J_BOLT_URL', 'bolt://neo4j:foobarbaz@localhost:7687')\n\n    # Make sure django_neomodel comes before your own apps\n    INSTALLED_APPS = (\n        # django.contrib.auth etc\n        'django_neomodel',\n        'yourapp'\n    )\n\nWrite your first node definition in `yourapp/models.py`::\n\n    from neomodel import StructuredNode, StringProperty, DateProperty\n\n    class Book(StructuredNode):\n        title = StringProperty(unique_index=True)\n        published = DateProperty()\n\nCreate any constraints or indexes for your labels. This needs to be done after you change your node definitions\nmuch like `manage.py migrate`::\n\n    $ python manage.py install_labels\n\nNow in a view `yourapp/views.py`::\n\n    from .models import Book\n\n    def get_books(request):\n        return render('yourapp/books.html', request, {'books': Book.nodes.all()})\n\nIn your `yourapp/admin.py`::\n\n    from django_neomodel import admin as neo_admin\n    from .models import Book\n\n    class BookAdmin(dj_admin.ModelAdmin):\n        list_display = (\"title\", \"created\")\n    neo_admin.register(Book, BookAdmin)\n\nAnd you're ready to go. Don't forget to check the neomodel_ documentation.\n\nModel forms\n===========\n\nSwitch the base class from `StructuredNode` to `DjangoNode` and add a 'Meta' class::\n\n    from datetime import datetime\n    from django_neomodel import DjangoNode\n    from neomodel import StructuredNode, StringProperty, DateTimeProperty, UniqueIdProperty\n\n    class Book(DjangoNode):\n        uid = UniqueIdProperty()\n        title = StringProperty(unique_index=True)\n        status = StringProperty(choices=(\n                ('Available', 'A'),\n                ('On loan', 'L'),\n                ('Damaged', 'D'),\n            ), default='Available')\n        created = DateTimeProperty(default=datetime.utcnow)\n\n        class Meta:\n            app_label = 'library'\n\nCreate a model form class for your `DjangoNode`::\n\n    class BookForm(ModelForm):\n        class Meta:\n            model = Book\n            fields = ['title', 'status']\n\nThis class may now be used just like any other Django form.\n\nSettings\n========\nThe following config options are available in django settings (default values shown).\nThese are mapped to neomodel.config as django is started::\n\n    NEOMODEL_NEO4J_BOLT_URL = 'bolt://neo4j:neo4j@localhost:7687'\n    NEOMODEL_SIGNALS = True\n    NEOMODEL_FORCE_TIMEZONE = False\n    NEOMODEL_MAX_CONNECTION_POOL_SIZE = 50\n\nSignals\n=======\nSignals work with `DjangoNode` sub-classes::\n\n    from django.db.models import signals\n    from django_neomodel import DjangoNode\n    from neomodel import StringProperty\n\n    class Book(DjangoNode):\n      title = StringProperty(unique_index=True)\n\n    def your_signal_func(sender, instance, signal, created):\n        pass\n\n    signals.post_save.connect(your_signal_func, sender=Book)\n\nThe following are supported: `pre_save`, `post_save`, `pre_delete`, `post_delete`.\nOn freshly created nodes `created=True` in the `post_save` signal argument.\n\nTesting\n=======\n\nYou can create a setup method which clears the database before executing each test::\n\n    from neomodel import db, clear_neo4j_database\n\n    class YourTestClass(DjangoTestCase):\n        def setUp(self):\n            clear_neo4j_database(db)\n\n        def test_something(self):\n            pass\n\nManagement Commands\n===================\n\nThe following django management commands have been included.\n\ninstall_labels\n--------------\nSetup constraints and indexes on labels for your node definitions. This should be executed after any schema changes::\n\n    $ python manage.py install_labels\n    Setting up labels and constraints...\n\n    Found tests.someapp.models.Book\n    + Creating unique constraint for title on label Book for class tests.someapp.models.Book\n    Finished 1 class(es).\n\nclear_neo4j\n-----------\nDelete all nodes in your database, warning there is no confirmation!\n\nRequirements\n============\n\n- Python 3.7+\n- neo4j 5.x, 4.4 (LTS)\n\n.. image:: https://badges.gitter.im/Join%20Chat.svg\n   :alt: Join the chat at https://gitter.im/robinedwards/neomodel\n   :target: https://gitter.im/robinedwards/neomodel?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge\n\nDocker Example\n===================\n\n\nUsing Docker Compose.\n\nCommands to setup Docker Container docker-entrypoint.sh::\n\n    # Go to tests\n    $ cd tests/\n    # Docker Command (Make sure Docker is running and up to date)\n    $ docker-compose up\n    # login in admin with username=admin password=1234\n\nGo to http://localhost:7474/browser/\n\nGo to http://localhost:8000/admin/\n\n\nRunning Tests\n===================\n\nSetup Neo4j Desktop with a local database with password 'foobarbaz' and version 5.x or 4.4.x (Neo4j LTS version).\n\nCommands to run tests::\n\n    # create local venv and install dependencies.\n    $ pip install -e '.[dev]'; export DJANGO_SETTINGS_MODULE=tests.settings;\n    $ tests/manage.py install_labels\n    $ tests/manage.py migrate\n    $ pytest\n\n    # example output:\n\n    platform darwin -- Python 3.9.0, pytest-6.1.2, py-1.9.0, pluggy-0.13.1\n    pick 0900469 Neo4J-update-t-4.1\n    collected 16 items\n\n    someapp/tests/test_atomicity.py .                                                                                                                                                                                                                      [  6%]\n    someapp/tests/test_commands.py ..                                                                                                                                                                                                                      [ 18%]\n    someapp/tests/test_model_form.py ...........                                                                                                                                                                                                           [ 87%]\n    someapp/tests/test_sanity.py .                                                                                                                                                                                                                         [ 93%]\n    someapp/tests/test_signals.py .\n    16 passed, 11 warnings in 1.62s\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Use Neo4j with Django!",
    "version": "0.1.1",
    "project_urls": {
        "repository": "http://github.com/robinedwards/django-neomodel"
    },
    "split_keywords": [
        "graph",
        "neo4j",
        "django",
        "plugin",
        "neomodel"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c2d487fb4bdb42d43492e87b54ab4d3625858a865a406df7927291068ff7218",
                "md5": "a600871011934579967b9d9fb21f8622",
                "sha256": "ca3ea5cfedfa4394df4644ec52360052d497ca5d3efc6d86d584b0af19bb4a83"
            },
            "downloads": -1,
            "filename": "django_neomodel-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a600871011934579967b9d9fb21f8622",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 18252,
            "upload_time": "2023-08-21T12:46:48",
            "upload_time_iso_8601": "2023-08-21T12:46:48.274856Z",
            "url": "https://files.pythonhosted.org/packages/2c/2d/487fb4bdb42d43492e87b54ab4d3625858a865a406df7927291068ff7218/django_neomodel-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "54b5f20236d02a65421f597cb20808b906ae48765f9d45613024d78b437bceea",
                "md5": "3e1f047a71c618df2e5d7ba2901cb967",
                "sha256": "9d05af4c099c10231acec7d7d2fd076318dca4d2402a71a7203347bffbe14b56"
            },
            "downloads": -1,
            "filename": "django_neomodel-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3e1f047a71c618df2e5d7ba2901cb967",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 24209,
            "upload_time": "2023-08-21T12:46:49",
            "upload_time_iso_8601": "2023-08-21T12:46:49.981696Z",
            "url": "https://files.pythonhosted.org/packages/54/b5/f20236d02a65421f597cb20808b906ae48765f9d45613024d78b437bceea/django_neomodel-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-21 12:46:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "robinedwards",
    "github_project": "django-neomodel",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "django-neomodel"
}
        
Elapsed time: 0.10229s