Tuesday, July 26, 2011

Randomizing in Django QuerySet and Python List

Django QuerySet:

my_queryset.order_by('?')

Python List:

import random

random.shuffle(my_list)


And then you can use slice to just keep n elements, e.g.
n = 5
my_queryset.order_by('?')[:n]

No comments:

Post a Comment