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?

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top