开发者社区 问答 正文

Flask-Sqlalchemy排序(order by)的代码是什么?

Flask-Sqlalchemy排序(order by)的代码是什么?

展开
收起
游客qzzytmszf3zhq 2021-11-17 22:34:25 334 分享 版权
1 条回答
写回答
取消 提交回答
  • python
    >>> from hello import db,Role,User
    # 升序(asc)
    >>> User.query.order_by(User.role_id).all()
    [<User u'john',Role id 1L>, <User u'susan',Role id 3L>, <User u'david',Role id 3L>]
    # 降序(desc)
    >>> User.query.order_by(User.role_id.desc()).all()
    [<User u'susan',Role id 3L>, <User u'david',Role id 3L>, <User u'john',Role id 1L>]
    
    
    2021-11-17 22:34:49
    赞同 展开评论
问答标签:
问答地址: