M
M
Michael2017-09-19 19:44:45
Django
Michael, 2017-09-19 19:44:45

How to select one random entry from database in django?

Need to select one random entry from the database, but falling under the filter?
For example Model.objects.get(moder=False) here you also need to select any 1 record.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stepan Krapivin, 2017-09-19
@DJWOMS1

use order_by('?')

qs = Model.objects.filter(moder=False).order_by("?")
random_el = qs.first()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question