fastapi-pagination: LimitOffsetParams don't work

This way pagination works fine:

from fastapi_pagination import pagination_params

When switching to limit-offset version:

from fastapi_pagination.limit_offset import pagination_params

same code and adjusted request fail with: "Page should be used with PaginationParams" Request:

http://0.0.0.0:8080/api/v1/call_records?date_from=2021-01-01&date_till=2021-02-01&limit=50&offset=0

Endpoint:

@router.get('/call_records', response_model=Page[CallRecord], dependencies=[Depends(pagination_params)], tags=['calls'])
@handle_exceptions
async def get_call_records(date_from: datetime.date, date_till: datetime.date):
    records = await core.get_call_records(database, date_from, date_till, None)
    page = paginate(records)
    return page

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Hmm, upon further testing it seems that the exporting keeps failing when Uvicorn reloads. Sometimes, it exports to OpenAPI schema correctly and is shown on the docs, but most of the time only a few routes are exported. Despite not being exported to the schema, the route still works as expected.

Will report back with code that can reproduce the error.

With new implementation, every Page class has Params type associated with it. Basically, Page class has __params_type__ attribute wich is pointing to Params class.

For instance, how to create change default Params:

from typing import TypeVar, Generic

from fastapi import Query

from fastapi_pagination.default import Page as BasePage, Params as BaseParams

T = TypeVar("T")


class Params(BaseParams):
    size: int = Query(500, gt=0, le=1_000, description="Page size")


class Page(BasePage[T], Generic[T]):
    __params_type__ = Params

Can you try to use this example?

from fastapi_pagination.limit_offset import pagination_params, Page
from fastapi_pagination import using_page

using_page(Page)  # set page to use

@router.get('/call_records', response_model=Page[CallRecord], dependencies=[Depends(pagination_params)], tags=['calls'])
@handle_exceptions
async def get_call_records(date_from: datetime.date, date_till: datetime.date):
    records = await core.get_call_records(database, date_from, date_till, None)
    page = paginate(records)
    return page

In case if you want to use another page model you must call using_page function with page model class.

from fastapi_pagination import using_page

using_page(Page)