How to pull three random records from a model in Django?
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
More answers about
"How to pull three random records from a model in Django?"
July 8th 19 at 16:10
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?
commented on July 8th 19 at 16:13
: randomly sorting
commented on July 8th 19 at 16:16
Find more questions by tags