1pk_list = [5, 7, 1, 3, 4]
2clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in enumerate(pk_list)])
3ordering = 'CASE %s END' % clauses
4queryset = Article.objects.filter(pk__in=pk_list).extra(
5 select={'ordering': ordering}, order_by=('ordering',))
6