Bootstrap FreeKB - Flask - Return SQLAlchemy results ascending, descending or shuffled
Flask - Return SQLAlchemy results ascending, descending or shuffled

Updated:   |  Flask articles

If you are not familiar with SQLAlchemy, check out my article Getting Started with SQLAlchemy.

This also assumes you are already able to Query records in a table using SQLAlchemy.

By default, the results will be returned in ascending order. order_by can be used to return results in descending order.

from sqlalchemy import desc
data = users.query.filter(users.email.like('%@%')).order_by(desc(users.id))

 

And here is how you could shuffle the results using func.random.

from sqlalchemy import func
data = users.query.filter(users.email.like('%@%')).order_by(func.random())

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c9294a in the box below so that we can be sure you are a human.