4

Possible Duplicate:
How to get something random in datastore (AppEngine)?

I'm trying to get 20 random records from a current set of 200, which I'm assuming will grow to 20,000+ within the month. Using Python in Google AppEngine.

  • It's for a iPhone API where a User can click "refresh" to get another random 20.

I've read a lot of answers, but none seem to either work correctly, or are random enough. Any help would be amazing.

My current code, just gets the latest 20.

items = ItemUser.all().filter('is_active =', True).order('-image_date').fetch(limit=2)

Some code examples would also be great as I'm still wrapping my head around Python, after coming from a solid Django background.

Community
  • 1
  • 1
Danny
  • 199
  • 10

3 Answers3

2

App engine doesn't support queries on an arbirary selection of entities. The best bet you might have is to add a property to the entity in question and give it a random integer value(in the range of, say 0 .. 2**64-1, at the time it's created.

then you'll just be fetching one entity at a time based on that value, picking some random value in that range and getting the first entity below the random number. to get 20, you'd have to perform 20 queries.

Since fetching entities one at a time is not a good use of your quotas, you should only generate the set of random entities periodically, not for every request.

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
1

From 'related questions': "How to get something random in datastore"

Linked post also has the code example.

Community
  • 1
  • 1
egor83
  • 1,199
  • 1
  • 13
  • 26
1

Not elegant or efficient, but:

import random

# Get all the keys, not the Entities
item_keys = ItemUser.all(keys_only=True).filter('is_active =', True).order('-image_date').fetch(2000)

# Get a random set of those keys, in this case 20 
random_keys = random.sample(item_keys, 20)

# Get those 20 Entities
items = db.get(random_keys)

Or your could keep a List of all Keys for ItemUser, and have a method that returns a random number of those keys from the List. Then you just do a batch get with that subset of Keys.

Will Curran
  • 6,959
  • 15
  • 59
  • 92