Skip to content

Instantly share code, notes, and snippets.

@mhluongo
Created May 7, 2012 15:24
Show Gist options
  • Save mhluongo/2628389 to your computer and use it in GitHub Desktop.
Save mhluongo/2628389 to your computer and use it in GitHub Desktop.
QuerySet relational filtering workaround (as of 5/7/2012. neo4django 03b247d7911753)
# a hack for `NodeA.objects.filter(nodeb__in=nodebnodes)`
from neo4django.db import connections
from neo4django.db.models.script_utils import LazyNode
# ...
b_nodes = set([...]) #your NodeB instances
b_node_ids = [b.id for b in b_nodes]
type_node_a = NodeA._type_node()
cypher_query = 'START b=node({b_ids}), t=node({type_node_id}) MATCH b -[r]-> a, t -[`<<INSTANCE>>`]->a RETURN a'
table = connections['default'].cypher(cypher_query, b_ids = b_node_ids, type_node_id = type_node_a.id)
models = [NodeA._neo4j_instance(LazyNode.from_dict(r[0])) for r in table['data']]
@alank64
Copy link

alank64 commented Jul 12, 2013

Big thanks for this example and the issue comment. Although the ORM layer is great for supporting other Django extensions that assume RDBMS, I would have to agree that Cypher query is a huge plus.

Little question: I've reviewed a few other neo4j extensions, both in the Rails and Python, and for most I've seen them simply put a property in the node that represents the class (ie element_type = 'Person') as opposed to the direction that neo4django takes, which is a class type reference node (ie model_name='Person' - [:<>] -> nodes). I have never been able to find a document that explains the pros and cons of both methods or Lucene comparison against the other. I also assumed that the way neo4django does it was the old way, before built integrated search engines were available such as Neo4j and Lucene. Do you mind sharing your views?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment