【问题标题】:In app engine, how does one get the key of an entity that has a certain attribute?在应用引擎中,如何获取具有特定属性的实体的键?
【发布时间】:2013-12-08 07:01:10
【问题描述】:

我想检查是否存在具有某个属性值的实体,如果存在,则获取该实体的键。

【问题讨论】:

    标签: python google-app-engine search jinja2


    【解决方案1】:

    看起来你想要做的是一个只有键的查询: https://developers.google.com/appengine/docs/python/datastore/queries#Python_Keys_only_queries

    您必须确保“某些属性”已编入索引,例如您可以对它们进行查询。

    【讨论】:

      【解决方案2】:

      是否只有一个实体具有该属性值?假设只有一个实体可以具有此属性值,您可以尝试以下操作:

      matched_entity = Entity.all().filter('your_attribute =', Value)
      if matched_entity.get():
          key = matched_entity.key()
          #your code for found item
      else:
          #your code for item not found
      

      值可以是整数、布尔值、字符串或之前定义的变量。这取决于您在模型定义中设置的属性类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 2014-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多