Question

I'm trying to link various entities to each other with KeyProperties in App Engine like this:

class ModelA(ndb.Model):
    mod_bs = ndb.KeyProperty(kind=ModelB, repeated=true)
    mod_cs = ndb.KeyProperty(kind=ModelC, repeated=true)
    # other properties

class ModelB(ndb.Model):
    mod_as = ndb.StringProperty(kind=ModelA, repeated=true)
    mod_cs = ndb.StringProperty(kind=ModelC, repeated=true)
    # other properties

class ModelC(ndb.Model):
    mod_cs = ndb.KeyProperty(kind=ModelA, repeated=true)
    mod_as = ndb.KeyProperty(kind=ModelB, repeated=true)
    # other properties

But I get an error saying that "ModelB" is undefined in this structure. Apparently, anything that is defined below where it is referenced is not recognized. So if I get rid of the kind assignments in ModelA and ModelB, the ones in ModelC are fine and everything runs. I need to reference them circularly, though, and it seems like that should work.

Am I doing something wrong?

Was it helpful?

Solution

In such cases, you can pass the kind as a string:

class ModelA(ndb.Model):
    mod_bs = ndb.KeyProperty(kind='ModelB', repeated=true)
    mod_cs = ndb.KeyProperty(kind='ModelC', repeated=true)
    # other properties
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top