【问题标题】:repeated StructuredProperty of ndb.GeoPtndb.GeoPt 的重复 StructuredProperty
【发布时间】:2014-10-24 06:39:32
【问题描述】:

如何将地理点作为结构化属性重复?

我的代码如下所示:

class MyModel(EndpointsModel):
    segments = ndb.StructuredProperty(ndb.GeoPt, repeated=True)

当我尝试运行代码并创建 MyModel 实例时,我收到以下错误:

AttributeError: type object 'GeoPt' has no attribute '_has_repeated'

How to find out if a model class is db or a ndb 似乎暗示 _has_repeated 是 ndb 模型特有的属性,https://cloud.google.com/appengine/docs/python/ndb/properties#structured 似乎表明 ndb.GeoPt 与 db.GeoPt 相同。

【问题讨论】:

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


    【解决方案1】:

    为什么要将 GeoPt 与结构化属性结合使用?只需使用这样的东西:

    class MyModel(EndpointsModel):
      segments = ndb.GeoPtProperty(repeated=True)
    

    但如果您想为每个 GeoPt 对象存储额外信息,请使用如下结构化属性:

    class GeoPtWithStruct(ndb.Model):
      geo = ndb.GeoPtProperty()
      bla = ndb.StringProperty()
    
    class MyModel(EndpointsModel):
      segments = ndb.StructuredProperty(GeoPtWithStruct, repeated=True)
    

    【讨论】:

    • 啊,当然。我正在用 GeoPt 替换自定义地理点模型,并忘记删除 StructuredProperty 包装器。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多