Categories
Python Answers

How to pull a random record using Python Django’s ORM?

Spread the love

To pull a random record using Python Django’s ORM, we can use the first method with order_by.

For instance, we write

MyModel.objects.order_by('?').first()

to call order_by with '?' to return the MyModel objects in random order.

And then we call first to pick the first one.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *