pythondjangoneo4jcypherneomodel

How to filter NeoModel Nodes by Relationships in Python


I have the 2 StructuredNodes User and Token as a one to one relationship. Coming from using Django for relational databases, if I want to get the User given a token, I'd simply do User.objects.get(token__key=token).

But using Neo4j with NeoModel, I'm struggling to find a way to do this simple query. Do I have to use cypher for this? if so how?

Also, when I have the User instance, I can do user.token.single() to get the Token instance, but the same doesn't work the other way around. token.user.single() returns CardinalityViolation: Expected: one relationship in a outgoing direction of type FOR_USER on node (49) of class 'Token', got: none. and token.user returns a neomodel.cardinality.One object.

class User(DjangoNode):
    uid = UniqueIdProperty()
    firstname = StringProperty(index=True, required=True)
    lastname = StringProperty(index=True, required=True)
    email = EmailProperty(unique_index=True, required=True)
    password = StringProperty(requried=True)

    token = RelationshipFrom('Token', 'OWNS_TOKEN', cardinality=One)

    def post_create(self):
        token = Token().save()
        self.token.connect(token)

class Token(DjangoNode):
    user = RelationshipTo('Token', 'FOR_USER', cardinality=One)

    key = StringProperty(unique_index=True, default=generate_key)
    created = DateTimeProperty(default_now=True)

Solution

  • Might be a bit late for the answer but,

    Shot in the dark here, but your RelationshipTo in your Token class is directed towards another Token, and not towards a specific user.