Skip to content

How to package PAGE data? #664

Answered by wu-clan
wu-clan asked this question in Q&A
May 16, 2023 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

Hi, @uriyyo

I've fine-tuned it to fit my global code and it looks like

DataT = TypeVar('DataT')
SchemaT = TypeVar('SchemaT')

class _PageData(GenericModel, Generic[DataT]):
    page_data: DataT | None = None

async def paging_data(db: AsyncSession, select: Select, page_data_schema: SchemaT) -> dict:
    _paginate = await paginate(db, select)
    page_data = _PageData[Page[page_data_schema]](page_data=_paginate).dict()['page_data']
    return page_data

Then use Depends(pagination_ctx(Page)) and my fixed ResponseModel, it looks like

@app.get(
    "/users",
    dependencies=[Depends(pagination_ctx(Page))],
)
async def get_all_users(db: Annotated[AsyncSession, Depends(get_db)]) -> ResponseModel

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@uriyyo
Comment options

@wu-clan
Comment options

Answer selected by uriyyo
@uriyyo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants