Neo4j and Django

907 views Asked by At

Is ist possible to use neomodel to make models in django? How do I have to integrate neo4j in django? I'm using Python 3, so neo4django isn't really an option. I'm new to both of them and at the moment I'm a little confused...

Thanks a lot! :3

3

There are 3 answers

3
rob On

Hey neomodel supports python 3 out of the box you can use it with or without django checkout the documentation here: http://neomodel.readthedocs.org/en/latest/

0
Ethan Posner On

Hey I know this was asked 8 years ago, but now there's a tool called neomodel which serves this exact purpose. It can also be combined with django-neomodel which allows for neomodel to be easily integrated into your django project.

Using django-neomodel, all you need to do is specify the URL of the database in your settings.py file like so:

NEOMODEL_NEO4J_BOLT_URL = 'bolt://{username}:{password}@{HOSTorIP}'

You can easily create models in your models.py file. Here are some examples for this neomodel documentation:

from neomodel import (StructuredNode, StringProperty,
                      UniqueIdProperty, IntegerProperty,
                      RelationshipTo)

class Country(StructuredNode):
    code = StringProperty(unique_index=True, required=True)

class Person(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty(unique_index=True)
    age = IntegerProperty(index=True, default=0)

    # traverse outgoing IS_FROM relations, inflate to Country objects
    country = RelationshipTo(Country, 'IS_FROM')

Then you can run python manage.py install_labels to do the equivalent of running migrations, or python manage.py clear_neo4j to clear all nodes from the database.

Nodes can be created like so:

from models import Person

john = Person(name="john", age=23).save()
frank = Person(name="frank", age=50).save()

canada = Country(code=5).save()

And relationships like so:

john.country.connect(canada)

And nodes/relationships can be retrieved as follows:

frank = Person.nodes.get(name='frank')
frank.age += 1
frank.save()

franks_country = frank.country
print(franks_country)
# {'code': 5}
0
Milan On
NEOMODEL_NEO4J_BOLT_URL = bolt://neo4j:password@localhost:7687
NEOMODEL_SIGNALS = True
NEOMODEL_FORCE_TIMEZONE = False
NEOMODEL_ENCRYPTED_CONNECTION = True
NEOMODEL_MAX_POOL_SIZE = 50

You need to add the above lines in your settings.py file to use Neo4j.