NDB多对多KeyProperty类引用不起作用

时间:2014-04-25 21:11:14

标签: python google-app-engine google-cloud-datastore app-engine-ndb

我正在尝试使用App Engine中的KeyProperties将各种实体相互链接,如下所示:

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

但是我得到一个错误,说“ModelB”在这个结构中是未定义的。显然,无法识别下面定义的任何内容。因此,如果我摆脱了ModelA和ModelB中的那种分配,那么ModelC中的那些分配就很好了,一切都在运行。不过,我需要循环地引用它们,看起来应该可行。

我做错了吗?

1 个答案:

答案 0 :(得分:1)

在这种情况下,您可以将该类型作为字符串传递:

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