How to order data in sqlalchemy by list

前端 未结 4 721
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-13 13:27

I have list of ID from external postgresql database.

A = [1,2,3,4,5,6,7,98,0]

I would to do query to database using SQLAlchemy, but I would

4条回答
  •  暖寄归人
    2021-01-13 13:56

    If you do not necessarily need to do this in SQL, you could simply sort the returned list of objects directly in python.

    Example (using python's sorted function)

    results = session.query(user).filter(user.id.in_(A)).all()
    results = sorted(results, key=lambda o: A.index(o.id))
    

提交回复
热议问题