How to extend Relationship class in neo4django

2019-07-19 05:44发布

问题:

I've seen that relationship properties are not yet implemented in neo4django. The workaround exposed in this thread is to have a new node type for each relationship with a property.

I can't afford too much calculations so I don't want to use this technique. While reading the source code I've seen, as the docstring of the Relationship class, this : """Extend to add properties to relationships."""

My question is, how to do that ? Where to start to add at least one property ?

Thanks

回答1:

Despite the docstring, this is still an open issue- the project's oldest, actually. There might be a way for you to pull it off by extending Relationship and BoundRelationship, but it won't be easy until I'm able to close that issue.

I would argue that this issue probably won't be a bottleneck using the project, since you can just give Neo4j more memory for the node store than the relationship store to account for it. YMMV of course.

I know it feels like a hack, though. If you really need custom relationship properties, the shortest path might be dropping down to the REST client level. To create relationships with properties, you could do something like

class Person(NodeModel):
  name = StringProperty()
  friends = Relationship('self', rel_type='friends_with')

pete = Person.objects.create(name='Pete')
dave = Person.objects.create(name='Dave')

# from the neo4j-rest-client [docs][2]
pete.node.relationships.create("friends_with", dave.node, since=123456789, introduced_at="Christmas party")

WDYT?