简体   繁体   中英

AppEngine: Query datastore for records with no condition for a specific property

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM