1

I've read this question: In Django, how do I select 100 random records from the database?
And tried to use Content.objects.all().order_by('?')[:30], but this will produce some duplicate items. So how could I select 30 unique random values from database?

Community
  • 1
  • 1
wong2
  • 34,358
  • 48
  • 134
  • 179
  • For millions of items: Can you modify your models? Do you need a different order **each time**? You can create an `int` index in your model and assign `randint(MAXINT)` to it. Then `order_by('randindex')`. Resetting the order is `O(n)` but doable (every hour/day for example). –  Jul 30 '12 at 10:16

1 Answers1

2

If you have a manageable number of entries in the database (ie not thousands), this will work, and even though it hits the db twice it will probably be much more efficient than order_by('?').

import random
content_pks = Content.objects.values_list('pk', flat=True)
selected_pks = random.sample(content_pks, 30)
content_objects = Content.objects.filter(pk__in=selected_pks)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Surely selecting thousands of ids should not be a problem. It's only a single column! – jathanism May 03 '11 at 15:55
  • Yes, probably should have said "millions". – Daniel Roseman May 03 '11 at 15:57
  • I tried this and found a problem of it. When you use `Content.objects.filter(pk__in=selected_pks)`, the result will be in the same order as they were in the database. I use `random.shuffle()` to solve it. – wong2 May 04 '11 at 10:21