简体   繁体   中英

NDB many-to-many KeyProperty kind reference not working

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM