-
Is it possible to not force the use of response_model as a Page class? If I want to unify the return style of all interfaces, fastapi-pagination looks abrupt Suppose my unified return model is class ResponseModel(BaseModel):
code: int = 200
msg: str = 'Success'
data: Any | None = None How to load page data under data parameters? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi @wu-clan, Sorry, I do not fully understand what you want to achieve. Could you show an example? |
Beta Was this translation helpful? Give feedback.
-
E.g.: This is a page api, Its return mode is Page[GetUserInfo] @user.get('', summary='get all users')
async def get_all_users(db: Annotated[AsyncSession, Depends(get_db)]) -> Page[GetUserInfo]:
users = select(User).order_by(User.time_joined.desc()).options(selectinload(User.roles).joinedload(Role.menus))
return await paginate(db, users)
If I want to unify the return model as ResponseModel @router.get('/{username}', summary='get userinfo by username')
async def get_userinfo(db: Annotated[AsyncSession, Depends(get_db)], username: str) -> ReasponseModel
userinfo = select(User).where(username=username).options(selectinload(User.roles).joinedload(Role.menus))
return ResponseModel(data=userinfo)
So how should the paging interface be set up? |
Beta Was this translation helpful? Give feedback.
Hi, @uriyyo
I've fine-tuned it to fit my global code and it looks like
Then use
Depends(pagination_ctx(Page))
and my fixed ResponseModel, it looks like