Skip to content

Instantly share code, notes, and snippets.

@andylolz
Created October 1, 2012 15:56
Show Gist options
  • Select an option

  • Save andylolz/3812667 to your computer and use it in GitHub Desktop.

Select an option

Save andylolz/3812667 to your computer and use it in GitHub Desktop.
Useful useful useful
def fetch_all(model_or_query):
'''Fetch all the entities of the given model, or satisfying the given query.
'''
if isinstance(model_or_query, type) and issubclass(model_or_query, db.Model):
query = model_or_query.all(keys_only=False)
elif isinstance(model_or_query, db.Query):
query = model_or_query
else:
raise Exception("model_or_query is neither a model nor a query")
results = []
while True:
to_fetch = query.fetch(500)
results += to_fetch
if len(to_fetch) < 500:
return results
query.with_cursor(query.cursor())
@andylolz
Copy link
Author

andylolz commented Oct 1, 2012

Fetch all donations:

fetch_all(models.Donation.all())

Fetch all donations for school $school:

fetch_all(models.Donation.all().filter("school", school))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment