Randell
Randell

Reputation: 6170

Zero results in Query/GqlQuery

How do I know if the results of my query either using the Query interface or the GqlQuery interface returned zero results? Would using .get() on zero results produce an error? If yes, what's the best way to handle it?

Upvotes: 2

Views: 951

Answers (2)

wings
wings

Reputation: 591

if a query returns no results, fetch() returns an empty list [] and get() returns None

in either case you can use the following:

if result:
    #handle the result
else:
    #no results were returned

Upvotes: 2

AutomatedTester
AutomatedTester

Reputation: 22418

when doing a get() if there are no results you will have an object containing None

I normally do

result = query.get()
if result is None:
  #do the following

or if you want to check that its not none then

if result is not None:
  #do the following

Upvotes: 5

Related Questions