Django get a random object

22,820

Solution 1

Just been looking at this. The line:

random_object = A.objects.order_by('?')[0]

has reportedly brought down many servers.

Unfortunately Erwans code caused an error on accessing non-sequential ids.

There is another short way to do this:

import random

items = list(Product.objects.all())

# change 3 to how many random items you want
random_items = random.sample(items, 3)
# if you want only a single random item
random_item = random.choice(items)

The good thing about this is that it handles non-sequential ids without error.

Solution 2

The second bit of code is correct, but can be slower, because in SQL that generates an ORDER BY RANDOM() clause that shuffles the entire set of results, and then takes a LIMIT based on that.

The first bit of code still has to evaluate the entire set of results. E.g., what if your random_idx is near the last possible index?

A better approach is to pick a random ID from your database, and choose that (which is a primary key lookup, so it's fast). We can't assume that our every id between 1 and MAX(id) is available, in the case that you've deleted something. So following is an approximation that works out well:

import random

# grab the max id in the database
max_id = A.objects.order_by('-id')[0].id

# grab a random possible id. we don't know if this id does exist in the database, though
random_id = random.randint(1, max_id + 1)

# return an object with that id, or the first object with an id greater than that one
# this is a fast lookup, because your primary key probably has a RANGE index.
random_object = A.objects.filter(id__gte=random_id)[0]

Solution 3

Improving on all of the above:

from random import choice

pks = A.objects.values_list('pk', flat=True)
random_pk = choice(pks)
random_obj = A.objects.get(pk=random_pk)

Solution 4

How about calculating maximal primary key and getting random pk?

The book ‘Django ORM Cookbook’ compares execution time of the following functions to get random object from a given model.

from django.db.models import Max
from myapp.models import Category

def get_random():
    return Category.objects.order_by("?").first()

def get_random3():
    max_id = Category.objects.all().aggregate(max_id=Max("id"))['max_id']
    while True:
        pk = random.randint(1, max_id)
        category = Category.objects.filter(pk=pk).first()
        if category:
            return category

Test was made on a million DB entries:

In [14]: timeit.timeit(get_random3, number=100)
Out[14]: 0.20055226399563253

In [15]: timeit.timeit(get_random, number=100)
Out[15]: 56.92513192095794

See source.

After seeing those results I started using the following snippet:

from django.db.models import Max
import random

def get_random_obj_from_queryset(queryset):
    max_pk = queryset.aggregate(max_pk=Max("pk"))['max_pk']
    while True:
        obj = queryset.filter(pk=random.randint(1, max_pk)).first()
        if obj:
            return obj

So far it did do the job as long as there is an id. Notice that the get_random3 (get_random_obj_from_queryset) function won’t work if you replace model id with uuid or something else. Also, if too many instances were deleted the while loop will slow the process down.

Solution 5

Yet another way:

pks = A.objects.values_list('pk', flat=True)
random_idx = randint(0, len(pks)-1)
random_obj = A.objects.get(pk=pks[random_idx])

Works even if there are larger gaps in the pks, for example if you want to filter the queryset before picking one of the remaining objects at random.

EDIT: fixed call of randint (thanks to @Quique). The stop arg is inclusive.

https://docs.python.org/3/library/random.html#random.randint

Share:
22,820
Erwan
Author by

Erwan

Seahorse programmer

Updated on July 19, 2021

Comments

  • Erwan
    Erwan almost 3 years

    I am trying to get a random object from a model A

    For now, it is working well with this code:

    random_idx = random.randint(0, A.objects.count() - 1)
    random_object = A.objects.all()[random_idx]
    

    But I feel this code is better:

    random_object = A.objects.order_by('?')[0]
    

    Which one is the best? Possible problem with deleted objects using the first code? Because, for example, I can have 10 objects but the object with the number 10 as id, is not existing anymore? Did I have misunderstood something in A.objects.all()[random_idx] ?