So i got the database.objects.all() and database.objects.get('name') but how would i got about getting one random item from the database. I'm having trouble trying to figure out how to get it ot select one random item.
So i got the database.objects.all() and database.objects.get('name') but how would i got about getting one random item from the database. I'm having trouble trying to figure out how to get it ot select one random item.
Selecting a random element from a list of all database objects isn't a goog solution as retrieving all elements of the database can have a big impact on performance, neither is using order_by('?')
as mentioned in the django documentation.
The best solution should be to retrieve an element with a random index:
import randomrandom_idx = random.randint(0, Model.objects.count() - 1)
random_obj = Model.objects.all()[random_idx]