Old way of customization
Old way of customizing pagination
Page.with_custom_options
and Page.with_params
methods are now deprecated.
They will be removed in the next major release.
Please use CustomizedPage
instead.
To customize pagination, you should use new CustomizedPage
annotated-like class.
It allows you to change default pagination in mypy
compatible way.
The main issue with old Page.with_custom_options
and Page.with_params
methods
was that they were not mypy
compatible.
It means that you could not use them with mypy
type checking, because they return
new class objects and mypy
does not support such ways.
Customization
First, let's import all necessary components:
from typing import Optional, TypeVar
from fastapi_pagination import Page, Params
from fastapi_pagination.customization import (
CustomizedPage,
UseAdditionalFields,
UseExcludedFields,
UseFieldsAliases,
UseIncludeTotal,
UseModelConfig,
UseName,
UseOptionalParams,
UseParams,
UseParamsFields,
)
T = TypeVar("T")
Now we can customize our page.
Change name
If you want to change default name of page class, you should use UseName
customizer:
- Now your class will be names 'IntPage' instead of 'CustomizedPage'.
Note
Everytime you customize page, name of the class will be changed to your-class-name
+ "Customized".
In order to change it, you should use UseName
customizer.
Change total behavior
By default, cursor-based page don't include total count of items, and offset-based page include it.
If you want to change this behavior, you should use UseIncludeTotal
customizer:
- Now when you will paginate using
PageNoTotal
class, it will not include total count of items.
Change params default values
If you want to change default values of pagination parameters, you should use UseParamsFields
customizer:
- Now when
size
parameter is not provided, it will be equal to 500.
Make params fields optional
If you want to change type of pagination parameters, you should use UseParams
customizer:
- Now all pagination parameters will be optional.
Change fields names
If you want use another name of field rather than default, you should use UseFieldsAliases
customizer:
- Now
total
field will be serialized ascount
.
Exclude fields
If you want to exclude some fields from serialization, you should use UseExcludedFields
customizer:
- Now
total
field will not be serialized.
Change pydantic model config
If you want to change pydantic model config, you should use UseModelConfig
customizer:
- Now
Page
class will haveanystr_lower
set toTrue
.
Change page params type
If you want to change type of page parameters, you should use UseParams
customizer:
class MyParams(Params): ... # your magic here
PageWithMyParams = CustomizedPage[
Page[T],
UseParams(MyParams), # (1)
]
- Now
Page.__params_type__
attribute will be point toMyParams
class.
Combine multiple customizers
You can use multiple customizers at once, just pass them as to regular Annotated
:
CustomPage = CustomizedPage[
Page[T],
UseName("CustomPage"),
UseIncludeTotal(False),
UseOptionalParams(),
] # (1)
- Now
CustomPage
will haveCustomPage
name, no total count of items, all params optional.