First you need to implement your own Page model. This model should inherit from fastapi_pagination.bases.AbstractPage.
You will need to implement create abstract classmethod.
Also,you need to create your own Params model or to use one of the existing ones.
from__future__importannotationsfromtypingimportTypeVar,Generic,Any,Sequence,Optionalfromfastapi_paginationimportParamsfromfastapi_pagination.basesimportAbstractPage,AbstractParamsT=TypeVar("T")classMyPage(AbstractPage[T],Generic[T]):results:list[T]totalResults:int__params_type__=Params@classmethoddefcreate(cls,items:Sequence[T],params:AbstractParams,*,total:Optional[int]=None,**kwargs:Any)->MyPage[T]:asserttotalisnotNone,"total must be provided"returncls(results=items,totalResults=total,)page=MyPage[int].create(range(10),Params(),total=100)print(page.model_dump_json(indent=4))
You can create your own Params model. In order to do that, you need to inherit from
fastapi_pagination.bases.AbstractParams and implement to_raw_params abstract method.
to_raw_params method should return an instance of BaseRawParams class. There are two types of BaseRawParamslimit-offset (RawParams) and cursor (CursorRawParams). Also, you can control if total value should
be calculated by passing include_total attribute to True or False.
Here if example of Params model for limit-offset pagination:
fromtypingimportAnnotatedfromfastapiimportQueryfromfastapi_pagination.basesimportAbstractParams,RawParamsclassMyParams(AbstractParams):pageNumber:Annotated[int,Query(...,ge=1)]pageSize:Annotated[int,Query(...,ge=1,le=100)]defto_raw_params(self)->RawParams:returnRawParams(limit=self.pageSize,offset=(self.pageNumber-1)*self.pageSize,include_total=False,# skip total calculation)
Here is an example of Params model for cursor pagination:
fromtypingimportAnnotatedfromfastapiimportQueryfromfastapi_pagination.basesimportAbstractParams,CursorRawParamsclassMyCursorParams(AbstractParams):cursor:Annotated[str,Query(...)]pageSize:Annotated[int,Query(...,ge=1,le=100)]defto_raw_params(self)->CursorRawParams:returnCursorRawParams(cursor=self.cursor,size=self.pageSize,include_total=False,# skip total calculation)