【问题标题】:Querying for a value existing in a model's list property in AppEngine在 AppEngine 中查询模型列表属性中存在的值
【发布时间】:2011-03-28 04:09:27
【问题描述】:

示例模型:

  class Foo(db.Model):
     id = db.IntegerProperty()
     bar = db.ListProperty(int, required=True)
  1. 如何使用 Query 或 GqlQuery 进行查询,以返回在其 bar 属性中具有给定值的所有 Foo 实体?

    李>
  2. 如果我有一个 id 列表,是否有一个过滤器可以返回 id 属性在该列表中的所有实体?

【问题讨论】:

    标签: google-app-engine bigtable


    【解决方案1】:

    1.

    如果您对列表属性使用等号查询,它将检查列表中的所有项目:

    search = 2
    results = Foo.all().filter('bar =', search).fetch()
    

    2.

    您可以使用 IN 过滤器,但请注意,这会在内部对列表中的每个项目进行数据存储查询,因此可能会很慢,并且每个请求最多有 30 个内部查询。

    items = [1, 2, 3]
    results = Foo.all().filter("id IN", items).fetch()
    

    有关 1 和 2 的详细信息,请参阅 Introducing Queries,有关 1 的更多详细信息,请参阅 ListProperty

    【讨论】:

      【解决方案2】:

      对于像我一样无法获得上述答案的人。

      使用 gae 版本 1.5.3

      results = Foo.all().filter('bar =', search).fetch()
      

      没有结果。但是

      results = Foo.all().filter('bar =', search).fetch(100)
      results = Foo.all().filter('bar =', search)
      

      给出结果。

      【讨论】:

        【解决方案3】:

        此外,您还可以使用 Gql.. 这可能是自提出问题以来发生的发展

        对于问题 1

        wheres = 2
        pageSize = 10
        qry = db.GqlQuery("SELECT * FROM Foo WHERE bar = :1", wheres)
        Foos = qry.fetch(pageSize)
        

        关于问题 2

        wheres = [ 1, 3, 44, 101 ]
        pagesize = 10
        qry = db.GqlQuery("SELECT * FROM Foo WHERE bar IN (:1)", wheres)
        Foos = qry.Fetch(pageSize)
        

        不过,请注意 in 查询。它实际上会执行 N 个子查询(IN 子句中的每个元素一个)。这是 Gql 文档:https://developers.google.com/appengine/docs/python/datastore/gqlreference

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-26
          • 2014-02-26
          • 2017-04-18
          • 2018-08-10
          • 2021-01-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多