I also have the same problem using a MySQL database. This is what I did:
my_list = [13,14,5,6,7] # convert my_list to str my_list_str = ','.join(map(str, my_list))
And here is what my query looks like:
checkpoints = ( db_session.query(Checkpoint) .filter(Checkpoint.id.in_(my_list)) .order_by('FIELD(id, ' + my_list_str + ')') .all() )
FIELD () is a native function in MySQL.
EDIT: means your request should look like this:
my_list_of_ids_str = ','.join(map(str, my_list_of_ids)) Shoe.query.filter(Shoe.id.in_(my_list_of_ids)).order_by('FIELD(id, ' + my_list_of_ids_str + ')').all()
Greetings
source share