def manual_order(qs, pks, pk_name='id'):
 if (not pks):
  return qs.none()
 return qs.filter(id__in=pks).extra(select={'_manual': ('FIELD(%s, %s)' % (pk_name, ','.join(map(str, pks))))}, order_by=['_manual'])

def manual_order ( qs , pks , pk_name = 'id' ) : if ( not pks ) : return qs . none ( ) return qs . filter ( id__in = pks ) . extra ( select = { '_manual' : ( 'FIELD(%s, %s)' % ( pk_name , ',' . join ( map ( str , pks ) ) ) ) } , order_by = [ '_manual' ] )
given a query set and a list of primary keys .