【问题标题】:How to access Google Cloud Datastore entity with dash in its name?如何访问名称中带有破折号的 Google Cloud Datastore 实体?
【发布时间】:2016-09-16 09:26:18
【问题描述】:

我正在处理一个 Google App Engine 项目,我需要访问一个名称包含破折号的 Datastore 实体,例如random-entity。我想在 Python 中做到这一点。由于random-entity 是类名的无效语法,因此我无法创建模型并像那样访问它。

那么我怎样才能访问这个实体呢?是否可以在不创建模型的情况下仅以 JSON 格式检索它?

请记住,重命名实体不是我正在处理的项目的选项。

【问题讨论】:

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


    【解决方案1】:

    如果您使用NDB library,则需要覆盖模型的class method _get_kind(cls),如下所示:

    class RandomEntity(ndb.Model):
    
      @classmethod
      def _get_kind(cls):
        return 'random-entity'
    
      # You can override property name as well
      random_name = ndb.StringProperty('random-name')
    

    编辑:在 _get_kind 之后添加了缺少的冒号

    【讨论】:

    • 我想这几乎可以回答我的问题,但我仍然无法让它工作。我想通过使用它的键来检索实体。我有类似key = Key('random-entity', '<key-value>') 的东西,但每当我尝试拨打key.get() 时,我都会得到KindError: No model class found for kind 'random-entity'. Did you forget to import it?RandomEntity 类在同一个文件中。有什么建议吗?
    • 其实我给了你错误的答案。源代码中的正确行是github.com/GoogleCloudPlatform/datastore-ndb-python/blob/master/… - 所以你需要覆盖@classmethod def _get_kind(cls) - 我会更新答案。抱歉误导!
    • Key(RandomEntity, '') 应该可以工作,它会自己使用它的内部种类名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2018-08-24
    • 2021-04-05
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    相关资源
    最近更新 更多