views:

78

answers:

1

Hi,

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 working 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

+2  A: 

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.

jbochi
Wouldn't ProductParts.gql("WHERE type = :type", type = "color") be the same as using an entire query?
fredrik
Yes, that would create the gql query for you, but you can also do `p = ProductParts.gql("WHERE type IN :types", types=["color", "style", "size"])`
jbochi