I try to pull a random object from the already filtered queryset this way:
Product.objects.filter(active=True).order_by('?').first()
Currently it is solved with .order_by('?')
but it seems to be very slow.
I try to implement the solution of this question to my view.
It does not work so I am not sure if I am doing it the right way.
products = Product.objects.filter(
active=True,
)
count = products.aggregate(count=Count('id'))['count']
random_index = randint(0, count - 1)
product = products[random_index]
This is my code now and it throws the ValueError:
ValueError: low >= high
Please, help me to speed it up.