views:

909

answers:

2

I need to get a count of records for a particular Model on app engine. How does one do it?

I bulk uploaded more than 4000 records but modelname.count() only shows me 1000.

+3  A: 

As of release 1.3.6, there is no longer a cap of 1,000 on count queries. Thus you can do the following to get a count beyond 1,000:

count = modelname.all(keys_only=True).count()

This will count all of your entities, which could be rather slow if you have a large number of entities. As a result, you should consider calling count() with some limit specified:

count = modelname.all(keys_only=True).count(some_upper_bound_suitable_for_you)
dar
Of course, you only have 300ms to do this :(
a paid nerd
300ms? Where did you get that figure? Regardless, though, this demonstrates why counting objects on the fly is not a good idea.
Nick Johnson
My guess is he meant 30,000ms. But that really isn't the case because if you're doing this because of the bulk uploader, you probably just run the count over the remote_api anyway - which AFAIK is not subject to the 30 second timeout.
dar
I added keys_only=True which is faster
Shay Erlichmen
A: 

In GAE a count will always make you page through the results when you have more than 1000 objects. The easiest way to deal with this problem is to add a counter property to your model or to a different counters table and update it every time you create a new object.

Federico Builes