How to pull three random records from a model in Django?

Guys, Hello.

Now make a simple side bar on the website with similar records.

The problem is this.

A model for articles. From this model need to get three random records. The number of entries and their id we don't know. What is the best way to implement?
July 8th 19 at 16:08
1 answer
July 8th 19 at 16:10
Solution
The simplest order_by('?')[:3], check if in this version there are problems with performance, then a sample of all the right id (that you can then cache), random.shuffle and [:3], and of the sample on these three id
And you can specify order_by('?') - sign of the question as it stands? - Shanie_Armstrong98 commented on July 8th 19 at 16:13
: randomly sorting https://docs.djangoproject.com/en/1.9/ref/models/q... - Garnett_Deckow commented on July 8th 19 at 16:16

Find more questions by tags PythonDjango