TAGS :Viewed: 4 - Published at: a few seconds ago

[ Google App Engine, Datastore object woes ]

So I'm not sure why, but my program can't seem to discern whether r is in results

I wonder as to whether or not this has anything to do with google datastore objects

        terms = query.split()
        res = db.GqlQuery("SELECT * FROM Assignment ORDER BY created DESC")
        results = []
        for term in terms:
            for r in res:
                #WHY THE CRAP DOESNT THIS WORK....
                #if r is in results... it still appends r to results
                if term in r.name and r not in results:
                if r.tags:
                    if term in r.tags and r not in results:

Answer 1

The line

for r in res:

is inside the loop

for term in terms:

res is a query. So when you call

for r in res:

it's really calling

for r in res.run():

So the code is executing the query and getting a new set of results every time through the outer loop. So these new result objects do not match the result objects you may have saved in a previous iteration of the outer loop.

You could change "results = []" to be a dictionary instead and use the keys of the entities as keys in the dictionary and then the comparison check would work. However, it's very inefficient to be executing this query many times inside the outer loop.

It looks like the goal of your code is to find out which Assignment entities contain the search terms in either the Assignment.name or Assignment.tags properties. If this is the case then I would suggest swapping things around and doing something like the following:

terms = query.split()
assignments = db.GqlQuery("SELECT * FROM Assignment ORDER BY created DESC")
assignmentsWithTerms = []

for assignment in assignments:
    for term in terms:
        if term in assignment.name or (assignment.tags and term in assignment.tags):

This approach will only execute the GqlQuery once so it will be much faster and cheaper. Also, as you loop through the results, it adds the assignment to the list as soon as the first term is found and then moves on, again making it faster.

I hope this helps!