import random
all_qs = Trending.objects.all()
if all_qs.exists(): # checks whether to return at least one record
# query all pk numbers and convert to a tuple
all_pks = tuple(all_qs.values_list("pk", flat=True))
# get a random pk number from the tuple
random_pk = random.choice(all_pks)
# get a object by the random pk
random_object = Trending.objects.get(pk=random_pk)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61600bb6-f877-4588-a799-0b29109d43d5n%40googlegroups.com.