fastapi-pagination
fastapi-pagination copied to clipboard
Support sorting by column name and it's direction
Great project! And I think this would be nice additional feature.
You should already be able to do this by specifying sorting attributes on the query being passed. From teh SQLALchemy example, for example:
@app.get("/users/default", response_model=Page[UserOut])
@app.get("/users/limit-offset", response_model=LimitOffsetPage[UserOut])
def get_users(db: Session = Depends(get_db)) -> Any:
return paginate(db.query(User).order_by(User.name.desc()))
Yes, you could do that, but wouldn't be great to have such close-theme feature out of the box? Also, probably, as said in #219 , there is room left for optimizations, that could be missed, with passing prepared queries.
Hi @Akkarine,
As @aledt said, you can specify order_by
when a passing query to paginate function:
@app.get("/users", response_model=Page[UserOut])
def get_users(db: Session = Depends(get_db)) -> Any:
return paginate(db.query(User).order_by(User.name.desc()))
New version 0.10.0
has minor optimization that will reset order_by
when executing count
query.
@Akkarine I am closing this issue.
Please, take a look at https://github.com/uriyyo/fastapi-pagination/issues/219#issuecomment-1250150968 to see the root cause of performance issues.