【问题标题】:Get name of subclass of ndb.StructuredProperty(subclass, repeated=True)获取 ndb.StructuredProperty(subclass, repeat=True) 的子类名称
【发布时间】:2013-09-20 03:32:42
【问题描述】:

给定一个NDB Datastore (Google AppEngine) 模型定义:

class Customer(ndb.Model):
    products = ndb.StructuredProperty(Product, repeated=True)

模型 Customer 定义了一个重复的子模型,称为 Product。如何获取子模型的名称,在本例中为“产品”?

【问题讨论】:

  • getattr(Customer, 'products') 返回StructuredProperty(Product, 'products', repeated=True)。也许这有帮助?

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


【解决方案1】:

你可以试试:

getattr(Customer, 'products')._modelclass

尽管如果您使用任何以 _ 为前缀的方法/属性,您将负责对 ndb 库进行底层 API 更改。

【讨论】:

  • 谢谢!我使用getattr(Customer, 'products')._modelclass.__name__ 来获取实际名称。我用谷歌搜索了“._modelclass”,但找不到任何关于它的文档。你能帮帮我吗?
  • 我查看了源代码:code.google.com/p/appengine-ndb-experiment/source/browse/ndb/…。此外,您可以使用 getattr(Customer, 'products')._modelclass == Products 进行比较,而不是依赖名称,除非您有不同的用例。
  • 谢谢!谢谢!这对我帮助很大!
  • NDB 中的 _ 属性应该是它的 API 的一部分,根据创建者的说法:stackoverflow.com/questions/10690418/… ... 几个月后,仍然没有记录! (直接指向问题的链接:code.google.com/p/appengine-ndb-experiment/issues/detail?id=187
  • 感谢您的澄清。我记得在 NDB 仍处于试验阶段时,我在某处读到过 _ 前缀属性的使用仅用于内部目的。
猜你喜欢
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多