How to provide the sort order in a variable in sqlalchemy?

只愿长相守 提交于 2019-12-02 01:25:39
Martijn Pieters

asc and desc are just objects, pick one based on the ordering you want:

direction = desc if order_type == 'desc' else asc

result = session.\
         query(Customer).\
         order_by(direction(getattr(Customer, sorting_column_name))).\
         all()

direction is bound to either asc or desc depending on the value of order_type, then used in building the query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!