Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Relationship models #1

mhluongo opened this Issue · 13 comments

6 participants

Matt Luongo ni-ka coffenbacher Stephen Muss James Pierre Romera
Matt Luongo

Using Django's "through=" standard, eg

class Pet(neo4django.NodeModel):
    name = neo4django.StringProperty()

class PetOwnership(neo4django.RelationshipModel):
    date_began = neo4django.DateProperty()

class Person(neo4django.NodeModel):
    pets = neo4django.Relationship(Pet, rel_type=neo4django.Outgoing.OWNS,
                                   related_name='owner', related_single=True,

as described in the docs (


Is any work beeing done on this issue? Could you describe how much needs to be done, where potential problems are? Would also appreciate some starting points to see whether I can contribute.

Matt Luongo

Definitely! This one has been given less attention, because my current data model can do without relationship properties not already managed by neo4django (like ordering, etc).

Some of the machinery necessary already exists in db/models/ for example, the RelationshipModel and BoundRelationshipModel classes written by @thobe are a start. Basically, to get this done, we need to be able to

  1. Write a couple tests demonstrating how the new feature should work. I've tried to keep features working the way a Django dev would expect.
  2. Create a relationship model class. The existing class should maybe inherit django.db.models.Model
  3. Allow that class to accept properties to be bound (add_field()).
  4. Make sure inheritance of these models works.
  5. Saving should create an edge with the appropriate properties, and insert indexed fields into Neo (like in

Basically the work is a synthesis of what's been done in and, while constantly checking to see if the built-in ORM has an easier way.

If you're comfortable with metaclasses (or are comfortable getting comfortable) and are ok with diving the Django ORM code, this would be an awesome contribution.

Matt Luongo

@ni-ka let me know if you decide to do it- I'd be happy to support the effort however I can. There are quite a few things I didn't touch on- eg, querying, deciding index scope for fields - but if you made the initial effort, I could take care of both querying, indexed fields, and some other details.

Matt Luongo

I think this could be accomplished (without querying or indexing) in ~300-400 lines- maybe less if you really leverage what Django already has (which would make for better code anyway).


Hi, thanks a lot for the quick and detailed reaction! In fact I'm only starting with Django, I evaluated rails / grails / django for a web project and then took the decision to go for the django path. Neo4j would be a great addition but is not absolutely necessary, but I've been interested in graph databases since a long time so I'm quite motivated to use them. I'm good in digging in new problems but I guess I need a bit of experience with Django ORM first. I hope I didnt wake to high expectation and also hope I can come back to this when I'm a bit more Djangotized!

Matt Luongo

Haha, no worries! If you'd like to take a crack at it anyway (or any of the other issues), I'd be happy to do code review/cleanup. I wasn't nearly so Djangotized before I started working on this project :)

If nothing else, I'll take your comment as a feature vote.

Matt Luongo mhluongo referenced this issue from a commit
Matt Luongo mhluongo Removed the RelationshipBase metaclass- it ...
represents an older effort at relationship models (#1).

Hey @mhluongo , thanks for your work on Neo4django, it is really great. Has there been any progress on this one? It is critical for my data model :) For now, I will workaround using NodeModel. Depending on how things go with my project, if there hasn't been any progress on this, I may take a shot at it.

Matt Luongo

@coffenbacher I've struggled to find time for this one, especially since it's planned for the next milestone, and I still have a ways to go on 0.1.9. I'm definitely open to a contribution. That said, the metaclass stuff is a bit sticky, so I'd be happy to help if you made some progress.

Stephen Muss

Would love to see this in neo4django. Unfortunately I'm not at a point where I could contribute towards it, but it seems like this could be extremely valuable to a number of people.


I also would love to see this. I have some time on weekends to look into this. Has any work been done towards supporting this feature?


Not from me unfortunately, although I may end up with a good situation in the next few months to work on this. Keep us updated if you try it! Good luck!

Pierre Romera Pirhoo referenced this issue in jplusplus/

Propriétés sur les relations / relations composites #154

Pierre Romera


Any chance someone here built something to get through relationships work with Neo4django ?

Matt Luongo

Unfortunately I've always just recommended people to add an intermediate node- I haven't seen any better solutions, and it'd take at least a weekend of work to hack something together I think. Anyone else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.