hXJaXNJxPrYRBxXiCzWc
hXJaXNJxPrYRBxXiCzWc

Reputation: 439

GAE (Python) Defining entity relationships in ndb


How would you go about creating a relationship between two different entity models?

When I try this I get an error:

class Spec(ndb.Model):
    userKey = ndb.KeyProperty(kind=User)

class User(ndb.Model):
    specs = ndb.KeyProperty(kind=Spec, repeated=True)

The error as I understand stems from referencing User before it is defined. I did the following to solve it, and I use a get_by_id, but I do not like this solution:

class Spec(ndb.Model):
    userKey = ndb.IntegerProperty()

class User(ndb.Model):
    specs = ndb.KeyProperty(kind=Spec, repeated=True)

How would you solve this so I can define my models as in the first example? Even better, how would you go about to define each class in its own file/module.

I tried following this article, but it seems to be be outdated and not relevant to ndb. https://developers.google.com/appengine/articles/modeling

Thank you

Upvotes: 0

Views: 836

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

As the docs show, the kind argument can be a string. So use kind='User'.

Upvotes: 2

Related Questions