1
votes

I am working on a django-nonrel app in Google App Engine.

I am trying to return items from a database in a random order. So I might have 100 items in my Items model. I wish to return a random selection of 20 items.

I have tried using:

Items.objects.order_by('?')[:20]

Except I get the following error:

Randomized ordering isn't supported by the backend

I take it this is a limitation of django-nonrel on GAE?

Is there an alternative method I could use for django-nonrel on GAE to get the same effect?

1
There have been several questions about this in SO, and the app engine mailing lists. Basically, you have a couple options: 1) add a random field, then query for entities around some randomly generated value; 2) try construct your keys such that you can randomly fetch some of them.Robert Kluin
Thanks Robert. In the end, since I had to return a list of all of the objects anyway, I copied the list and used random.shuffle() to shuffle the list order. Then returned the required number back to the view. I tried one of your approaches before I found out about the shuffle() command, but the shuffle just seemed easiest. Thanks againiali

1 Answers

2
votes

I've manage to find a workaround for this.

I just used python's random.shuffle(LIST_ITEM) as a way fo doing this.

It was actually very simple in the end.