Skip to content

Instantly share code, notes, and snippets.

Last active December 17, 2015 08:29
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save edmenendez/5580829 to your computer and use it in GitHub Desktop.
Django manager to pull a random row from a query
import datetime
import random
from django.db import models
from django.db.models import Count
class RandomManager(models.Manager):
def __init__(self, *args, **kwargs):
self.when_last_cached = - datetime.timedelta(minutes=10)
return super(RandomManager, self).__init__(*args, **kwargs)
def random_filter(self):
Override this if you need to filter on something
return self.all()
def random(self):
now =
if self.when_last_cached < now - datetime.timedelta(minutes=5):
self.when_last_cached = now
self.count = self.random_filter().aggregate(count=Count('id'))['count']
# Try 3 times then fail
i = 0
while True:
random_index = random.randint(0, self.count - 1)
return self.random_filter()[random_index]
except IndexError:
if i > 2:
raise IndexError
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment