SQLAlchemy ORDER BY FIELD()
This may not be a very satisfying solution, but how about using a case expression instead of order by fields
:
sqlalchemy.orm.Query(AlphabetTable) \
.filter(AlphabetTable.letter.in_("gack")) \
.order_by(sqlalchemy.sql.expression.case(((AlphabetTable.letter == "g", 1),
(AlphabetTable.letter == "a", 2),
(AlphabetTable.letter == "c", 3),
(AlphabetTable.letter == "k", 4))))
A sqlalchemy func expression can be used to generate the order by field
clause:
session.query(AlphabetTable) \
.filter(AlphabetTable.letter.in_("gack")) \
.order_by(sqlalchemy.func.field(AlphabetTable.letter, *"gack"))