views:

30

answers:

1

Is there a way to substitute:

def get_objects(attr1,attr2,..):
    objects = Entities.all()

    if attr1 != None:
        objects.filter('attr1',attr1)
    if attr2 != None:
        objects.filter('attr2',attr2)
    ....
    return objects

With a single query:

Entities.all().filter('attr1',attr1).filter('attr2',attr2)

By using some sort of 'match all' sign ( maybe a regexp query )? The problem with the first query is that ( apart from being ugly ) it creates indexes for all possible filter sequences.

+1  A: 

The datastore doesn't support regex queries or OR queries.

However, if you're only using equality filters, indexes shouldn't be automatically created; these types of queries can be served using a merge-join strategy as long as the number of filters remains low (if you try to add too many filters, you'll get an error indicating that the existing indexes can't be used to execute the query efficiently; however, trying to add the required indexes in a case like this will usually result in the exploding indexes problem.)

The ugliness in the first approach can probably be solved by passing a list to your function instead of individual variables, then using a list comprehension instead of a bunch of if statements.

Wooble
Unfortunately I have to query for datetime properties so not only equality filters are used. Thanks for the second tip.
Piotr Duda