1

How I can sort rundom the list 'articles_list' when I get it from db?

def article_theme(request, articleblock_id=2):
    return render_to_response('article_theme.html', 
   {"ArticleBlock_name": ArticleBlock.objects.get(id=articleblock_id), 
   'articles_list': Articles.objects.filter
   (articles_articleblock_id=articleblock_id)})
  • [There is a related post](http://stackoverflow.com/questions/962619/how-to-pull-a-random-record-using-djangos-orm) which talks about picking a random row from database. It also links to the findings as to why using `order_by('?')` as suggested in django documentation is inefficient. – AKS Apr 06 '17 at 10:18

1 Answers1

2

Here it is:

Articles.objects.filter(articles_articleblock_id=articleblock_id).order_by('?')
lch
  • 2,028
  • 2
  • 25
  • 46