0

In Django, How can i select n random objects in no particular order out of a queryset (already pulled out from the database) of say m objects (ofcourse m>n)?

There can be many ways but is there a django specific way for this?

sprksh
  • 2,204
  • 2
  • 26
  • 43
  • It's not a duplicate of that question. I already have a queryset selecte. Out of that i want to select random objects – sprksh May 07 '16 at 04:39
  • 1
    If you already have a queryset you could still do `order_by('?')` and then get n random objects. – AKS May 07 '16 at 04:47
  • 1
    Yes, you can do order_by('?')[:n] to get the required result. If the queryset is large and you have to select a small number then you can select n random numbers between 0 and (m-1) and the do list operations to get those particular entries from the queryset – Rohit May 07 '16 at 05:09

0 Answers0