python - GAE : Making many queries into one

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

我有一个包含名为“type”的字符串属性的productpart 数据库。 我想做的是获取给定类型的所有产品(有时不止一种类型)。

我尝试过使用GAE过滤方法,但无法使其正常工作。

我唯一的解决方案是为每种类型创建一个新的 db.GqlQuery。

我需要按类型获取每个内容的原因是为了在客户端以不同的方式显示它们?

有没有一种方法可以只使用一个查询来实现这一点?

目前看起来像这样:

    productPartsEntries = {
                             'color' :  db.GqlQuery("SELECT * FROM ProductParts WHERE type = :type", type = 'color'),
                             'style' :  db.GqlQuery("SELECT * FROM ProductParts WHERE type = :type", type = 'style'),
                             'size' :  db.GqlQuery("SELECT * FROM ProductParts WHERE type = :type", type = 'size')
// add more....
}

..弗雷德里克

最佳答案

您可以使用 IN 运算符。它将创建三个不同的查询,并在场景下将结果分组在一起。请参阅docs :

GQL does not have an OR operator. However, it does have an IN operator, which provides a limited form of OR.

The IN operator compares value of a property to each item in a list. The IN operator is equivalent to many = queries, one for each value, that are ORed together. An entity whose value for the given property equals any of the values in the list can be returned for the query.

Note: The IN and != operators use multiple queries behind the scenes. For example, the IN operator executes a separate underlying datastore query for every item in the list. The entities returned are a result of the cross-product of all the underlying datastore queries and are de-duplicated. A maximum of 30 datastore queries are allowed for any single GQL query.

关于python - GAE : Making many queries into one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2077034/

相关文章:

python - 使用 Python 在 Google App Engine 中的应用启动时加载全局可访问的单例

python - Pandas 将字典嵌套到数据框

python - 如何模拟随机正态变量Python

python - 我怎样才能划分 Pandas 数据框?

用于 GAE 的 Java 与 Python

android - Android Webview-HTTP错误504网关超时

python - 替换python中列表之间的元素

google-app-engine - 通过 Cronjob 备份 Datastore

java - Google App Engine 数据存储建模

java - session 超时在 Google App Engine 上不起作用