Skip to content

Instantly share code, notes, and snippets.

@mhluongo
Created February 29, 2012 19:52
Show Gist options
  • Save mhluongo/1943955 to your computer and use it in GitHub Desktop.
Save mhluongo/1943955 to your computer and use it in GitHub Desktop.
An example neo4django Person model with connections() based on select_related
class Person(models.NodeModel):
name = models.StringProperty()
knows = models.Relationship('Person',
rel_type = neo4django.Outgoing.KNOWS,
related_name = 'is_known')
def connections(self, depth=1):
from neo4django.db.models import query
from itertools import chain
query.execute_select_related(models=[self], max_depth=depth)
people = list(self.knows.all())
for i in xrange(depth):
for p in people: yield p
people = list(chain(*(p.knows.all() for p in people)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment