Reznov Reznov - 10 months ago 42
Python Question

How to sort by likes or views Flask?

I have a problem sorting Authors by


Lets me first show you my codes snippets :

from sqlalchemy import func
def authors():
page = request.args.get('page', 1, type=int)
pagination = Author.query.order_by(func.count(Author.likes)).paginate(page, per_page=app.config['AUTHORS_PER_PAGE'], error_out=False)

Here am using the func.count() function to sort the authors ordered by

The problem is am getting just the most liked author , in fact i have 12 of them in the database each one has his likes, so i need to sort them all by likes from the highest one to the lowest .

I tried a different ways but without avail , please any help !


Remove func.count and just order by Author.likes, it's already a count. func.count is for counting groups of things. Currently, you're grouping everything into one group and so you only get one item back.