This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
MIT License
879
stars
143
forks
source link
Arbitrarily ordering on paginated GET requests #18
User story: Alli the API consumer wants to be able to sort heroes by certain fields and choose if they are ascending or descending order.
async def get_multi_paginated_ordered(
self,
*,
params: Optional[Params] = Params(),
ordered_by: Optional[str] = None,
order: Optional[IOrderEnum] = IOrderEnum.ascendent,
query: Optional[Union[T, Select[T]]] = None,
db_session: Optional[AsyncSession] = None
) -> Page[ModelType]:
db_session = db_session or db.session
if ordered_by not in self.model.__table__.columns:
ordered_by = self.model.id
if query == None:
if order == order.ascendent:
query = select(self.model).order_by(self.model[ordered_by].asc()) # <---- SQLModelMetaclass is not subscriptable
else:
query = select(self.model).order_by(self.model.desc()) <---- type object {T_type_goes_here} has no attribute 'desc'
return await paginate(db_session, query, params)
Here is what I have so far.
Trying to figure out how I can pass in a specific field to be ordered into the model to order by that is validated as an existing field in the pydantic model.
Any ideas?
User story: Alli the API consumer wants to be able to sort heroes by certain fields and choose if they are ascending or descending order.
Here is what I have so far. Trying to figure out how I can pass in a specific field to be ordered into the model to order by that is validated as an existing field in the pydantic model. Any ideas?