Created
February 29, 2012 19:52
-
-
Save mhluongo/1943955 to your computer and use it in GitHub Desktop.
An example neo4django Person model with connections() based on select_related
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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