开发者

GAE: Making many queries into one

开发者 https://www.devze.com 2022-12-16 09:29 出处:网络
I have a productpart database containing a string property named \'type\'. What I\'m trying to do is to get all products by a given type (sometimes more then one type).

I have a productpart database containing a string property named 'type'. What I'm trying to do is to get all products by a given type (sometimes more then one type).

I've tried to use GAE filter method but can't get it to work properly.

The only solution I've got w开发者_如何学JAVAorking is to make a new db.GqlQuery for each type.

The reason I need to fetch each by type is to display them in different 's on the client side?

Is there a way to use just one query for this?

Currently it looks like this :

    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....
}

..fredrik


You can use the IN operator. It would create the three different queries and group the results together for you under the scenes. See the 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号