gpt4 book ai didi

python - GAE : Making many queries into one

转载 作者:行者123 更新时间:2023-12-01 06:18:11 25 4
gpt4 key购买 nike

我有一个包含名为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com