views:

51

answers:

1

i want to do a query that a user may or may not select a filter, but i don't want to create 2 indexes (tables).

value=self.request.get('filter')
if value:
    results=Entity.all().filter('p1 =','v1').filter('p2 =','v2').filter('filter_property =',value)
else:
    results=Entity.all().filter('p1 =','v1').filter('p2 =','v2')

i could order the filter_property. like this in the last line:

    results=Entity.all().filter('p1 =','v1').filter('p2 =','v2').order('filter_property')

this would be bad if i could or could not filter p1 (property1) and p2 (property2). i would like to do something like:

value = self.request.get('filter')
if value:
    operator = '='
else:
    operator = '!='
results=Entity.all().filter('p1 =','v1').filter('p2 =','v2').filter('filter_property '+operator,value).order('p4')

".order('p4')" will fail BadArgumentError with the operand !=.

what should i do?

A: 

It sounds like you've got a good handle on the alternatives. You can add order clauses to replace filters if you want the datastore to use the same index for each; otherwise, you're stuck with having multiple indexes.

Nick Johnson