문제

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