【问题标题】:NDB many-to-many KeyProperty kind reference not workingNDB 多对多 KeyProperty 类型参考不起作用
【发布时间】:2014-04-25 21:11:14
【问题描述】:

我正在尝试使用 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 中的那些就很好,一切都可以运行。不过,我需要循环引用它们,而且看起来应该可以。

我是不是做错了什么?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多